Receive JSON questions-php Tutorial

Source: Internet
Author: User
I have just learned how to submit JSON in CURL for receiving JSON questions,
However, no data is returned from the receiving interface. after the member submits the login information, the interface returns the data. if 00 is returned, the login fails if 11 is returned.

Log on to the website and go to other websites. Failed all afternoon. Give me an example.

Thank you very much.


Reply to discussion (solution)

Submitted server:
If (logon successful ){
Echo ('00 ');
} Else {// failed
Echo ('11 ');
}

Your program:
$ Ch = curl_init ();
$ Url = "submit the server address and parameters ";
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ A = curl_exec ($ ch );
Curl_close ($ ch );
If ($ a = '00 '){
Code executed successfully;
} Elseif ($ a = '11 '){
Logon failed
}

In general, I don't know if it's what you think.

Submitted server:
If (logon successful ){
Echo ('00 ');
} Else {// failed
Echo ('11 ');
}

Your program:
$ Ch = curl_init ();
$ Url = "submit the server address and parameters ";
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ A = curl_exec ($ ch );
Curl_close ($ ch );
If ($ a = '00 '){
Code executed successfully;
} Elseif ($ a = '11 '){
Logon failed
}

In general, I don't know if it's what you think.

Receive two parameters A and B. when A = 00 indicates that the login is successful, jump to the URL in B. Can I directly jump to another website?

$ ArrayData = array ("name" => "Hagrid", "age" => "36"); $ sendJsonData = json_encode ($ arrayData ); $ ch = curl_init ('http: // 127.0.0.1/jietu/test. php '); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ sendJsonData); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'Content-Length :'. strlen ($ sendJsonData); $ ret = curl_exec ($ ch); if ($ ret = '00') {code executed successfully ;} elseif ($ ret = '11') {logon failed}

$ ArrayData = array ("name" => "Hagrid", "age" => "36"); $ sendJsonData = json_encode ($ arrayData ); $ ch = curl_init ('http: // 127.0.0.1/jietu/test. php '); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ sendJsonData); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('content-Type: application/json', 'Content-Length :'. strlen ($ sendJsonData); $ ret = curl_exec ($ ch); if ($ ret = '00') {code executed successfully ;} elseif ($ ret = '11') {logon failed}


This was originally written in this way. login failed
{"Aa": "00", "bb": "http://www.xxx.com/index "}
After writing for half a day, you will not be able to get aa or bb, and the logon is successful only when aa = 00. I just found out

Var_dump ($ ret = curl_exec ($ ch); you can output your values to see

Var_dump ($ ret = curl_exec ($ ch); you can output your values to see

Boolean false

Now post data is successful, just return value {"aa": "00", "bb": "http://www.xxx.com/index"} How to get it

Json_decode

As the moderator said, the return value is converted using json_decode.

Json_decode

More confused... Can you give an example?
Now I have successfully submitted data to the interface, how to retrieve this {"aa": "00", "bb": "http://www.xxx.com/index "}

It's easy to understand... Just exploring


Var_dump ($ ret = curl_exec ($ ch); you can output your values to see

Boolean false

Now post data is successful, just return value {"aa": "00", "bb": "http://www.xxx.com/index"} How to get it
$ Json_str = '{"aa": "00", "bb": "http://www.xxx.com/index "}';
$ Arr = json_decode ($ json_str );
Print_r ($ arr );

After you $ ret = curl_exec ($ ch)
Echo $ ret; {"aa": "00", "bb": "http://www.xxx.com/index "}
Indicates that you have received a json string.
So

$t = json_decode($ret);print_r($t);/*stdClass Object(    [aa] => 00    [bb] => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00    [bb] => http://www.xxx.com/index)*/

You are using json. why don't you even know json-related functions?

After you $ ret = curl_exec ($ ch)
Echo $ ret; {"aa": "00", "bb": "http://www.xxx.com/index "}
Indicates that you have received a json string.
So

$t = json_decode($ret);print_r($t);/*stdClass Object(    [aa] => 00    [bb] => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00    [bb] => http://www.xxx.com/index)*/

You are using json. why don't you even know json-related functions?

Let's see... It's that simple. thank you very much for preparing to close the post. thanks to the moderator and the code.

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.