内容类型不随CURLOPT_HTTPHEADERS而改变 [英] Content-type not changing with CURLOPT_HTTPHEADERS
问题描述
我尝试使用以下代码将一些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访问控制允许原产地:* Content-长度:120日期:Fri,18 Mar 2011 19:03:23 GMT {code:error.indexdefinition.invalid,message:为/ define提供无效的内容。错误:文件过早结束..}
我发现这篇博文似乎是相关的 - 即使我在<$ c $中指定了 ContentType
,它似乎仍在发送 text / plain
c> CURLOPT_HTTPHEADERS 为 application / json
。但添加 http_build_query
没有帮助。
感谢您提供任何帮助!
我相信应该是 HTTPHEADER
,而不是 HTTPHEADERS
。
http://php.net/manual/en/function.curl-setopt.php
I'm trying to POST some JSON to a web service with cURL, using the following code:
$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);
I get the following in return:
string(282) "HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Content-Type: text/plain Content-Length: 120 Date: Fri, 18 Mar 2011 19:03:23 GMT {"code":"error.indexdefinition.invalid","message":"Invalid content provided for /define. Error:Premature end of file.."}"
I found this blog post that seems to be related -- it does seem to be sending text/plain
even though I've specified the ContentType
in CURLOPT_HTTPHEADERS
as application/json
. But adding http_build_query
hasn't helped.
Thanks in advance for any help!
I believe it should be HTTPHEADER
, not HTTPHEADERS
.
http://php.net/manual/en/function.curl-setopt.php
这篇关于内容类型不随CURLOPT_HTTPHEADERS而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!