PHP with Curl Post sends JSON and returns JSON data instance

Source: Internet
Author: User

<?PHP$arr=Array(    ' Subject ' = ' course ', ' loginName ' = ' durriya ', ' Password ' and ' 123 ');//JSON can also$data _string= Json_encode ($arr);//Normal array also line//$data _string = $arr;Echo $data _string;//echo ' <br> ';//curl verification Success$ch= Curl_init ("http://test.api.com/"); 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);if(Curl_errno ($ch)) {    PrintCurl_error ($ch);} Curl_close ($ch);Echo $result;

The curl above will still be able to access the success

//Curl Verification Successful$curl=curl_init (); curl_setopt ($curl, Curlopt_url, "http://hzgwyw.gensee.com/integration/site/training/room/created"); curl_setopt ($curl, Curlopt_ssl_verifypeer,false); curl_setopt ($curl, Curlopt_ssl_verifyhost,false); curl_setopt ($curl, curlopt_post,1); curl_setopt ($curl, Curlopt_postfields,$data _string); curl_setopt ($curl, Curlopt_header, 0); curl_setopt ($curl, Curlopt_httpheader,Array(        ' Content-type:application/json; Charset=utf-8 ', ' content-length: '.strlen($data _string)) ; curl_setopt ($curl, Curlopt_returntransfer, 1);$res= Curl_exec ($curl); Curl_close ($curl);Echo $res;

The above is in a random PHP page can be implemented, the project test in thinkphp does not support the definition of the head, and then there is a new curl format can also be submitted successfully, but the form of the array can be implemented

$ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$url ); curl_setopt ($ch, Curlopt_post, 1 ); curl_setopt ($ch, Curlopt_header, 0 ); curl_setopt ($ch, Curlopt_returntransfer, 1 ); curl_setopt ($ch, Curlopt_postfields,$data _string ); $response= Curl_exec ($ch); if(Curl_errno ($ch)) {            PrintCurl_error ($ch); } curl_close ($ch);

Problems encountered in the process of doing interface testing:

How to submit using a form: (Can be JSON, or it can be an array)

1) After committing, convert value to array---> Array escape json_encode ($arr); JSON data can be implemented,

2) after committing the value into an array---> do not escape directly in the form of arrays: both methods can be implemented

Assign an array of values directly to the variable (it is not possible to JSON data)

All of the above three proven data can be exported to JSON format

<?PHP$data=Array("Subject" = "Nihao", "StartDate" and "2016-10-12 22:22:22", "loginName" and "=" [Email prot Ected] ', ' password ' = ' hzgwyw ',              );$data _string=$data;$url= "Http://hzgwyw.gensee.com/integration/site/training/room/created";$ch=curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_postfields,$data _string );$response= Curl_exec ($ch);if(Curl_errno ($ch)) {    PrintCurl_error ($ch);} Curl_close ($ch);Echo $response;
View Code

PHP with Curl Post sends JSON and returns JSON data instance

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.