【发布时间】: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 并没有帮助。
提前感谢您的帮助!
【问题讨论】: