【问题标题】:Content-type not changing with CURLOPT_HTTPHEADERS内容类型不随 CURLOPT_HTTPHEADERS 改变
【发布时间】:2011-07-18 11:48:18
【问题描述】:

我正在尝试使用以下代码将一些 JSON 发布到带有 cURL 的 Web 服务:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://index.yolink.com/index/define?o=json&ak=APIKEY');
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));

$data = array(
  'ignore-robots' => 'false',
      'language' => 'english',
  'crawl-delay' => '0',
  'depth' => '3',
  'root' => array('url' => 'http://bartleby.com/')
);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result=curl_exec($ch);
var_dump($result);

我得到以下回报:

string(282) "HTTP/1.1 200 OK 服务器: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Content-Type: text/plain Content-Length: 120 Date: Fri, 18 Mar 2011 19:格林威治标准时间 03:23 {"code":"error.indexdefinition.invalid","message":"为 /define 提供的内容无效。错误:文件过早结束.."}"

我发现this blog post 似乎是相关的——它似乎确实发送了text/plain,即使我已将CURLOPT_HTTPHEADERS 中的ContentType 指定为application/json。但是添加 http_build_query 并没有帮助。

提前感谢您的帮助!

【问题讨论】:

    标签: php json post curl


    【解决方案1】:

    我认为应该是HTTPHEADER,而不是HTTPHEADERS

    http://php.net/manual/en/function.curl-setopt.php

    【讨论】:

    • 太棒了,考虑到我在官方文档中发现它拼写为 HTTPHEADERS。我被同样的问题发疯了。
    • 真的吗?我链接的文档拼写正确(尽管其中一个 cmets 错误地引用了HTTPHEADERS)。
    • @Toast 如果你引用php.net/manual/en/function.curl-setopt.php#88538,我现在已经修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2011-01-05
    • 2012-02-28
    相关资源
    最近更新 更多