Search code examples
phppostzend-frameworkzend-http-client

Content type-error when using Zend_Http_Client


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.


Solution

  • 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);