Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

php - Content-type not changing with CURLOPT_HTTPHEADERS

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe it should be HTTPHEADER, not HTTPHEADERS.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...