我正在尝试使用 Zend_Http_Client 和 POST 将数据发送到 Google Analytic 的收集器。我有一个数组
$postParams
,其中包括我的跟踪 ID、cid 和命中类型,我通过 setParameterPost()
将此数组的值添加到我的客户端。
这是我行动的相关部分:
$client = new Zend_Http_Client('https://ssl.google-analytics.com/debug/collect');
foreach ($postParams as $postParam => $postValue) {
$client->setParameterPost($postParam, $postValue);
}
$response = $client->request();
调用此脚本时出现以下错误:
无法自动处理内容类型 ''。请使用 Zend_Http_Client::setRawData 发送此类内容。
在Zend_Http_Client中的
_prepareBody()
方法中抛出。当我在那里添加 echo($this->enctype); die();
时,我收到 NULL
.
我会在我的代码中添加
$client->setEncType();
,但数据很简单。setRawData
吗?
提前致谢!
更新:
$client->setParameterPost('postParams', $postParams);
也行不通。它抛出同样的错误。
这个答案让我回到正轨:https://stackoverflow.com/a/7407491/3218828
$rawData = '';
foreach ($postParams as $postParam => $postValue) {
if ($rawData !== '') {
$rawData .= '&';
}
$rawData .= $postParam . '%5B%5D=' . $postValue;
}
$client = new Zend_Http_Client();
$client->setRawData($rawData);
$client->setUri('https://ssl.google-analytics.com/debug/collect');
$client->request(Zend_Http_Client::GET);