Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-type not changing with CURLOPT_HTTPHEADERS

Tags:

json

post

php

curl

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!

like image 758
Jami Avatar asked Mar 18 '11 21:03

Jami


1 Answers

I believe it should be HTTPHEADER, not HTTPHEADERS.

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

like image 126
Ryre Avatar answered Nov 02 '22 09:11

Ryre