How does php obtain remote JSon content and post some parameters?

Source: Internet
Author: User
Ask php how to obtain remote JSon content and post some parameters. ask php how to obtain remote JSon content and post some parameters. Objective: php requests remote php pages (the page is json content ), submit some parameters (such as the name and pwd fields) and return the json data to your example. for example, solution ------------------ PHPcode $ datafile_ge, ask php how to obtain the remote JSon content and post some parameters.
Ask php how to obtain remote JSon content and post some parameters

Purpose: php requests a remote php page (the page is json content), submits some parameters (such as the name and pwd fields), and returns the json

I hope you can give an example.

------ Solution --------------------
PHP code
$ Data = file_get_contents ($ url); // get the target page content $ t = json_decode ($ data, 1); // Convert it to a PHP array // process... $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ urlo); // data sending address curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); // The sent data array curl_exec ($ ch );
------ Solution --------------------
PHP code
Function requrest ($ url, $ posts) {if (is_array ($ posts )&&! Empty ($ posts) {foreach ($ posts as $ key => $ value) {$ post [] = $ key. '= '. urlencode ($ value) ;}$ posts = implode ('&', $ post) ;}$ curl = curl_init (); $ options = array (CURLOPT_URL => $ url, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 10, rows => true, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $ posts, CURLOPT_USERAGENT => 'mozilla/5.0 (Windows NT 5.1; rv: 5.0) Gecko/20100101 Firefox/5.0 '); cu Rl_setopt_array ($ curl, $ options); $ retval = curl_exec ($ curl); return $ retval;} $ posts = array ('name' => 'root ', 'pwd' => '20140901'); $ retval = request ($ url, $ posts); if ($ retval! = False) {$ Arr = json_decode ($ retval, true );}
------ Solution --------------------
If your server is linux or unix, you can also use a tool:

PHP code
System ("curl-d 'name = xx & password = xxx' 'http: // www.xx.com/xx.php'>./tmp"); while (! File_exists ('./tmp') {$ jsoncode = file_get_contents ('./tmp'); sleep (1);} var_dump (json_decode ($ jsoncode ));
------ Solution --------------------
1. you can use file_get_contents
Reference video:
Http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/2009/0416/810.html


2. you can also use curl
Reference video:
Http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/2010/0621/4795.html
Http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/2010/0628/4848.html

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.