Php sends json data instances through curlpost

Source: Internet
Author: User
Using phpcurl to send json data is the same as other data in curlpost. next I will summarize several examples of sending json data in curlpost, hoping to deepen your understanding of curlp

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 code:

  1. $ Data = array ("name" => "Hagrid", "age" => "36 ");
  2. $ Data_string = json_encode ($ data );
  3.  
  4. $ Ch = curl_init ('http: // api. local/rest/users ');
  5. Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
  6. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data_string );
  7. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  8. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (
  9. 'Content-Type: application/json ',
  10. 'Content-Length: '. strlen ($ data_string ))
  11. );
  12.  
  13. $ Result = curl_exec ($ ch );

Example 2: The code is as follows:

  1. Function http_post_data ($ url, $ data_string ){
  2. $ Ch = curl_init ();
  3. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  4. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  5. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data_string );
  6. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (
  7. 'Content-Type: application/json; charset = utf-8 ',
  8. 'Content-Length: '. strlen ($ data_string ))
  9. );
  10. Ob_start ();
  11. Curl_exec ($ ch );
  12. $ Return_content = ob_get_contents ();
  13. Ob_end_clean ();
  14. $ Return_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
  15. Return array ($ return_code, $ return_content );
  16. }
  17. $ Url = "http://xx.xx.cn ";
  18. $ Data = json_encode (array ('a' => 1, 'B' => 2 ));
  19. List ($ return_code, $ return_content) = http_post_data ($ url, $ data );

Example 3 code:

  1. $ Data = '{
  2. "Button ":[
  3. {
  4. "Type": "click ",
  5. "Name": "Today's songs ",
  6. "Key": "V1001_TODAY_MUSIC"
  7. },
  8. {
  9. "Type": "click ",
  10. "Name": "Introduction to singers ",
  11. "Key": "V1001_TODAY_SINGER"
  12. },
  13. {
  14. "Name": "menu ",
  15. "Sub_button ":[
  16. {
  17. "Type": "click ",
  18. "Name": "hello word ",
  19. "Key": "V1001_HELLO_WORLD"
  20. },
  21. {
  22. "Type": "click ",
  23. "Name": "Like Us ",
  24. "Key": "V1001_GOOD"
  25. }]
  26. }]
  27. }';
  28. $ Ch = curl_init ($ urlcon); // request URL
  29. Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
  30. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // $ data JSON string
  31. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  32. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'Content-Length: '. strlen ($ data )));
  33. $ Data = curl_exec ($ ch );
  34. 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.