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.