Jam*_*ami 6 php post json curl
我正在尝试使用以下代码将一些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);
Run Code Online (Sandbox Code Playgroud)
我得到以下回报:
string(282)"HTTP/1.1 200 OK服务器:Apache-Coyote/1.1访问控制允许 - 来源:*内容类型:text/plain内容 - 长度:120日期:星期五,2011年3月18日19:03:23 GMT {"code":"error.indexdefinition.invalid","message":"为/ define提供的内容无效.错误:文件过早结束.."}"
我发现这篇博文似乎是相关的 - text/plain即使我已经指定了ContentTypein CURLOPT_HTTPHEADERS,它似乎确实在发送application/json.但添加http_build_query没有帮助.
在此先感谢您的帮助!