Post JSON using Curl
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!
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'));
You can also use
curl --silent --request POST --header 'Content-Type: application/json' \
--data $updates \
-k http://URL_to_which_you_want_to_post
精彩评论