Php sends json data instances through curl post

Source: Internet
Author: User

Using php curl to send json data is the same as other data in curl post. Next I will summarize several examples of sending json data in curl post, I hope you can better understand the curl post json data.

Example 1

The Code is as follows: Copy code

$ Data = array ("name" => "Hagrid", "age" => "36 ");
$ Data_string = json_encode ($ data );
 
$ Ch = curl_init ('HTTP: // api. local/rest/users ');
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data_string );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/json ',
'Content-Length: '. strlen ($ data_string ))
);
 
$ Result = curl_exec ($ ch );

Example 2

The Code is as follows: Copy code

Function http_post_data ($ url, $ data_string ){

$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data_string );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/json; charset = UTF-8 ',
'Content-Length: '. strlen ($ data_string ))
);
Ob_start ();
Curl_exec ($ ch );
$ Return_content = ob_get_contents ();
Ob_end_clean ();

$ Return_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
Return array ($ return_code, $ return_content );
}

$ Url = "http://xx.xx.cn ";
$ Data = json_encode (array ('A' => 1, 'B' => 2 ));

List ($ return_code, $ return_content) = http_post_data ($ url, $ data );

Example 3

The Code is as follows: Copy code

$ Data = '{
"Button ":[
{
"Type": "click ",
"Name": "Today's songs ",
"Key": "V1001_TODAY_MUSIC"
},
{
"Type": "click ",
"Name": "Introduction to singers ",
"Key": "V1001_TODAY_SINGER"
},
{
"Name": "menu ",
"Sub_button ":[
{
"Type": "click ",
"Name": "hello word ",
"Key": "V1001_HELLO_WORLD"
},
{
"Type": "click ",
"Name": "like us ",
"Key": "V1001_GOOD"
}]
}]
}';

$ Ch = curl_init ($ urlcon); // request URL
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // $ data JSON string
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'content-Length: '. strlen ($ data )));
$ Data = curl_exec ($ ch );
Print_r ($ data); // returned after successful creation: {"errcode": 0, "errmsg": "OK "}

Summary: The Core code is Content-Type: application/json. This is the file format Type.

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.