tags:

views:

88

answers:

1

Hi Everyone!

I am receiving a JSON which I can receive using php://input and I need to post it back to a different URL, but I'm not sure how to format it. Here's how I receive it:

$updates = file_get_contents("php://input");

I could json_decode it and then parse out the array so that it would fit a normal POST-like request such as hello=world&stack=overflow etc. But is it possible to pass the JSON just into a curl post like this:

curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $updates);

and then the client could just php://input to grab the data again? The reason I ask is because the client says he is not receiving the data, it may be he doesn't have it set up correctly. I've noticed when you do something like this through a command line you have to include -H in the command so that the data is interpretted as normal POST, perhaps there's a libcurl CURLOPT I need to set?

Thanks!

+1  A: 

Well folks, I have figured this one out!

To send a post as a different content-type (ie.. application/json or text/xml) add this setopt call

curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));

Pete Herbert Penito