Content type-error when using Zend_Http_Client

守給你的承諾、 提交于 2020-01-03 10:26:07

问题


I'm trying to send data to Google Analytic's collector with Zend_Http_Client and POST. I have an array $postParams that's including my tracking-ID, cid and hit types and I add the values of this array to my client via setParameterPost().

Here's the relevant part of my Action:

$client = new Zend_Http_Client('https://ssl.google-analytics.com/debug/collect');
foreach ($postParams as $postParam => $postValue) {
    $client->setParameterPost($postParam, $postValue);
}
$response = $client->request();

When calling this script I get the following error:

Cannot handle content type '' automatically. Please use Zend_Http_Client::setRawData to send this kind of content.

It's thrown in the _prepareBody() method in Zend_Http_Client. When I'm adding an echo($this->enctype); die(); there, I receive NULL.

I'd add $client->setEncType(); to my code but the data is plain.
Has anyone an idea what I'm missing here? Do I really have to use setRawData?

Thanks in advance!

Update: $client->setParameterPost('postParams', $postParams); won't work too. It throws the same error.


回答1:


This answer brought me back on track: 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);


来源:https://stackoverflow.com/questions/30095209/content-type-error-when-using-zend-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!