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