使用Zend_Http_Client时的内容类型错误

时间:2015-05-07 08:03:45

标签: php post zend-framework zend-http-client

我尝试使用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);也没有成功。它会抛出相同的错误。

1 个答案:

答案 0 :(得分:3)

这个回答让我回到正轨: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);
相关问题