In php, curl simulates post to send json and receives json

Source: Internet
Author: User
Description: Simulate the request server data locally. The request data format is json. The data returned by the server is also json. Ajax simulation is successful & amp; lt; script & amp; gt; $. ajax ({type: & quot; POST & quot;, crossDomain: true, url: & #039; ******* & #039;, data: {& #039; command & #039 ;... description:
Simulate the request server data locally. The request data is in json format, and the data returned by the server is also in json format.
Ajax simulation is successful.

Script $. ajax ({type: "POST", crossDomain: true, url: 'http: // ******* ', data: {'command ': 'test'}, success: function (e) {console. log (e); // here the log is json}, dataType: 'json'}); script

Curl fails.
$ Url = 'HTTP ://*******';
$ Param = "{'command': 'test '}";
$ Ch = curl_init ($ url); // request URL
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ param); // $ data JSON string
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'content-Length: '. strlen ($ param )));
$ Data = curl_exec ($ ch); // The data printed here is empty.

Reply content:

Description:
Simulate the request server data locally. The request data is in json format, and the data returned by the server is also in json format.
Ajax simulation is successful.

Script $. ajax ({type: "POST", crossDomain: true, url: 'http: // ******* ', data: {'command ': 'test'}, success: function (e) {console. log (e); // here the log is json}, dataType: 'json'}); script

Curl fails.
$ Url = 'HTTP ://*******';
$ Param = "{'command': 'test '}";
$ Ch = curl_init ($ url); // request URL
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ param); // $ data JSON string
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'content-Length: '. strlen ($ param )));
$ Data = curl_exec ($ ch); // The data printed here is empty.

$ Param = "{'command': 'test '}";
This line is changed to the PHP array key value form.
When curl simulates POST, the POST parameter should be passed as an array.

How did I write the final code of lz? I replaced param with an array and finally got the data empty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.