首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未设置PHP cURL内容类型

未设置PHP cURL内容类型
EN

Stack Overflow用户
提问于 2013-10-28 21:41:21
回答 1查看 105.6K关注 0票数 31

我有一个简单的web服务,我想要连接它。为了发布一些XML,这将在web服务端进行适当的处理,我需要准备一个适当的请求。我使用cURL来做这样的事情:

代码语言:javascript
复制
try {
    $ch = curl_init();

    if (FALSE === $ch)
        throw new Exception('failed to initialize');

    curl_setopt($ch, CURLOPT_URL,"192.168.1.37");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/xml',
                                            'Connection: Keep-Alive'
                                            ));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch, CURLOPT_PROXY, '');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:  "));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);


    $request=  curl_getinfo($ch);
    var_dump($request);


    $content = curl_exec($ch);


    if (FALSE === $content)
        throw new Exception(curl_error($ch), curl_errno($ch));



} catch(Exception $e) {

    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),E_USER_ERROR);

}

一切都会好起来的。首先,我显示了来自curl_getinfo($ch);的请求体,然后显示了来自$content变量的响应。

对发送到服务器的请求的唯一限制是Content-Type头-它必须是application/xml,否则服务器将响应一个错误(这不是我的主意,我对此无能为力)

下面是输出请求:

array(21) { ["url"]=> string(68) "192.168.1.37" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } }

响应是HTTP 400 Bad Request

我可以从中看到的是["content_type"]=> NULL,但是我在我的PHP代码中设置了这个变量...

谁能告诉我我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-28 21:50:15

您正在设置HTTP_HEADER两次:

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/xml',
                                            'Connection: Keep-Alive'
                                            ));

curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:  "));

我想这就是问题所在。第二次设置时,会删除第一个设置。

票数 62
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19636367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档