The local impersonation Request server data, the request data format is JSON, the server returns the data is also JSON. Due to the specificity of the requirements, such as synchronizing the client's bulk data to the cloud, the data submitted to the server may be multidimensional array data. This data needs to be organized and submitted in a certain data encoding (JSON format). So that the server can handle it well.
Client curl simulates the commit code.
function http ($url, $data = NULL, $json = False)
{
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_ssl_verifypeer, false);
curl_setopt ($curl, Curlopt_ssl_verifyhost, false);
if (!empty ($data)) {
if ($json && Is_array ($data)) {
$data = Json_encode ($data);
}
curl_setopt ($curl, Curlopt_post, 1);
curl_setopt ($curl, Curlopt_postfields, $data);
if ($json) {//Send JSON data
curl_setopt ($curl, Curlopt_header, 0);
curl_setopt ($curl, Curlopt_httpheader,
Array
' Content-type:application/json; Charset=utf-8 ',
' Content-length: '. Strlen ($data))
);
}
}
curl_setopt ($curl, Curlopt_returntransfer, 1);
$res = curl_exec ($curl);
$errorno = Curl_errno ($curl);
if ($errorno) {
Return Array (' errorno ' = False, ' errmsg ' = $errorno);
}
Curl_close ($curl);
Return Json_decode ($res, true);
}
Parameter description:
$url: server receives processing URL
$data: Post data in array form
$json: Whether to submit in JSON (1: Yes, 0: NO)
The server side obtains the post data code:
Print_r ($_post);
The last data fetched is a null value.
Search the Internet to find PHP by default only recognizes the application/x-www.form-urlencoded standard data type, the modification header information also has no result ... Data can only be obtained in the following ways
The first of these methods
$post = $GLOBALS [' Http_raw_post_data '];
The second method of
$post = file_get_contents ("Php://input");
After the last modification, the data can be received.
PHP in Curl Analog post sends JSON and receives JSON