PHP in Curl Analog post sends JSON and receives JSON

Source: Internet
Author: User

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

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.