【问题标题】:Content type-error when using Zend_Http_Client使用 Zend_Http_Client 时的内容类型错误
【发布时间】:2023-03-21 16:52:01
【问题描述】:

我正在尝试使用 Zend_Http_Client 和 POST 将数据发送到 Google Analytic 的收集器。我有一个数组 $postParams,其中包括我的跟踪 ID、cid 和命中类型,我通过 setParameterPost() 将此数组的值添加到我的客户端。

这是我的 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();

调用此脚本时出现以下错误:

无法自动处理内容类型“”。请使用 Zend_Http_Client::setRawData 发送此类内容。

在 Zend_Http_Client 的 _prepareBody() 方法中抛出。当我在那里添加echo($this->enctype); die(); 时,我会收到NULL

我会将$client->setEncType(); 添加到我的代码中,但数据很简单。
有人知道我在这里缺少什么吗?我真的必须使用setRawData吗?

提前致谢!

更新:$client->setParameterPost('postParams', $postParams); 也不起作用。它会引发同样的错误。

【问题讨论】:

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


    【解决方案1】:

    这个答案让我重回正轨: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);
    

    【讨论】:

    • 谢谢,@david-caunt !
    猜你喜欢
    • 2012-02-07
    • 2012-06-01
    • 2012-03-27
    • 2015-09-04
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多