JSON message using Curl

I get JSON, which I can get with php: // input, and I need to send it back to another url, but I'm not sure how to format it. This is how I get it:

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

I could json_decode and then parse the array so that it matches normal POST-like requests like hello = world & stack = overflow etc. But is it possible to pass JSON only in curl post like this

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

and then the client can just enter php: // to capture the data again? The reason I ask is because the client says that it is not receiving data, it may not have configured it correctly. I noticed that when you do something like this via the command line, you must include -H in the command so that the data is interpreted as regular POST, maybe libcurl CURLOPT should be installed there?

Thank!

+3
source share
2 answers

Well guys, I figured it out!

To send a message as a different type of content (e.g. application / json or text / xml), add this call to setopt

curl_setopt ($ h, CURLOPT_HTTPHEADERS, array ("Content-Type: Application / JSON '));

+1

curl --silent --request POST --header 'Content-Type: application/json' \
--data $updates \
-k http://URL_to_which_you_want_to_post
0

Source: https://habr.com/ru/post/1760343/


All Articles