Sophie McCarrell
Sophie McCarrell

Reputation: 2871

My content-type is being overwritten on my curl request

EDIT: It turns out the data was being sent just fine, but viewing the body in PHP wasn't working correctly. See my answer for more info.

I'm attempting to post json data to a server.I just need the content-type to be json and the body to have a string in json format. My problem is that the content-type is staying as text/html and no matter what I try I can't get that content-type to change. I read numerous stack overflow answers, and they all seem like they should work, but they don't. My code is below.

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));      
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  curl_exec($ch);

On the receiving end I'm simply printing getallheaders and get_file_contents('file://phpinput').

Why isn't my content-type going through correctly?

Here is an example output if it helps:

string 'HTTP/1.1 200 OK
Date: Wed, 17 Apr 2013 16:24:56 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 PHP/5.4.4
X-Powered-By: PHP/5.4.4
Content-Length: 71
Content-Type: text/html

Array{"level1a":{"level2":{"a":2,"b":3}},"level2b":["111","333","999"]}' (length=273)

Upvotes: 0

Views: 1311

Answers (2)

Bass Jobsen
Bass Jobsen

Reputation: 49054

You need to create two php files, a client (client.php) and a server (server.php).

The server reads json requests and send back a (json) response. Your client sends json to te server and reads the response.

You server.php need to be availible on a webserver http://localhost/server.php. You can run your client from the same or an other server http://localhost/client.php. You can also run the client from the command line php -f client.php

server.php:

<?
// read json input
$input = json_decode(file_get_contents('php://input'));
$response = array('text'=>'Your name is: '.$input->{'name'});
header("Content-type: application/json");
// send response
echo json_encode($response);
exit;

client.php:

<?
$data = array('name'=>'Jason'); // input
$ch = curl_init('http://localhost/server.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
list($headers, $content) = explode("\r\n\r\n", $result, 2);
$php = json_decode($content);
echo 'Response: ' . $php->text;
// if you want to know
var_dump($headers);
exit;

Upvotes: 1

Sophie McCarrell
Sophie McCarrell

Reputation: 2871

So it turns out nothing was wrong. I'm not sure where the "Array" part came from, but the API I was posting to received the data fine. I think it would be great if someone posted a way to see exactly what is being sent out by a curl request, that way people could have an easier time troubleshooting than I had.

Upvotes: 0

Related Questions