Original: Php:6 get and POST request Send method
In the I94web blog, I tried to Chang and say two social comment boxes, and later abandoned the smooth speech, unsafe.
I need to crawl the number of comments from the article remotely and then deposit it into the local database, whether it's a word or a lot. For more said, the requested format is as follows:
//Get comment number, parameter is Article ID function getcommcount ($postid) { $jsondata = file_get_contents ("http://api.duoshuo.com/ threads/counts.json?short_name=i94web&threads= $postid "); //Set True to return an array, not set or false to return an object $resjson = Json_decode ($jsondata,true); return $resjson [' response '] [$postid] [' comments '];}
There are a number of ways to do this for remote requests. Today, the LZ has collected six kinds, for everyone's reference.
1, use file_get_contents to get the content:
<?php$url=' http://www.ido321.com/'; $html = file_get_contents ($url); echo $html;? >
2, open the URL with fopen, get the way to obtain
' R '); Stream_get_meta_data ($FP); while (!feof ($FP)) {$result. = fgets ($fp, 1024);} Echo "URL body: $result"; fclose ($FP);
3. Use file_get_contents to post to get content:
$data =Array(' foo '=' Bar '); $data = Http_build_query ($data); $opts =Array(' http '=Array(' method '=' POST ',' Header '="Content-type:application/x-www-form-urlencodedrn".' content-length: '. Strlen ($data).' RN ',' content '= $data)); $context = Stream_context_create ($opts); $html = file_get_contents (' http://localhost/e/admin/test.html ',false, $context);Echo$html;
4. Open the URL with the Fsockopen function, get the complete data in Get mode, including header and Body,fsockopen need php.ini allow_url_fopen option to open
functionGet_url ($url, $cookie =false{$url = Parse_url ($url); $query = $url [path].'? '. $url [query];Echo ' Query: '. $query; $fp = Fsockopen ($url [host], $url [port]? $url [port]:80, $errno, $errstr, 30);if(! $fp) {return false;}Else{$request =' GET $query http/1.1rn '; $request. =' Host: $url [host]rn '; $request. =' Connection:closern ';if($cookie) $request. =' Cookie: $cookien '; $request. =' RN '; Fwrite ($fp, $request); while([email protected] ($FP)) {$result. = @fgets ($fp, 1024);} Fclose ($FP);return$result;}}//Get the HTML part of the URL and remove the headerfunctionGeturlhtml ($url, $cookie =false{$rowdata = Get_url ($url, $cookie);if($rowdata) {$body = Stristr ($rowdata,' Rnrn '); $body =substr ($body, 4,strlen ($body));return$body;}return false;}
5. Open the URL with the Fsockopen function to get the complete data by post, including header and body
functionHttp_post ($URL, $data, $cookie, $referrer ="'){//parsing the given URL$URL _info=parse_url ($URL);//Building referrerif($referrer = ="')//If not given with this script as referrer$referrer =' 111 ';//making string from $dataforeach($data as$key and $value) $values []=' $key = '. UrlEncode ($value); $data _string=implode (' & ', $values);//Find out which port was needed–if not given use standard (=80)if(!isset($URL _info[' Port '])) $URL _info[' Port ']=80;//Building post-request:$request. ="POST". $URL _info[' path ']."http/1.1n"; $request. ="Host:". $URL _info[' Host ']."n"; $request. ="Referer: $referern"; $request. ="Content-type:application/x-www-form-urlencodedn"; $request. =' content-length: '. strlen ($data _string)."n"; $request. =' Connection:closen '; $request. =' Cookie: $cookien '; $request. =' n '; $request. = $data _string.' n '; $fp = Fsockopen ($URL _info[' Host '], $URL _info[' Port ']); Fputs ($fp, $request); while(!feof ($FP)) {$result. = fgets ($fp, 1024);} Fclose ($FP);return$result;}
6. Using the Curl Library, before using the Curl Library, you may need to check to see if the php.ini has already turned on the Curl extension
$ch = Curl_init (); $timeout = 5;curl_setopt ($ch, Curlopt_url, ' http://www.ido321.com/'); echo $file _contents;
Original starting: http://www.ido321.com/1297.html
Next: CSS 3 display: Box type detailed
Php:6 kinds of Get and post request sending methods