I have tried to speak 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:
Gets the number of comments, the argument is the article Idfunction Getcommcount ($postid) { $jsondata = file_get_contents ("Http://api.duoshuo.com/threads /counts.json?short_name=i94web&threads= $postid "); Sets 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
$fp = fopen ($url, ' 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
function Get_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,); if (! $fp) { return false;} else {$request = ' GET $query http/1.1rn '; $request. = ' Host: $url [Host]rn '; $request. = ' Connection:closern '; if ($cookie) $r equest.= ' cookies: $cookien '; $request. = ' rn '; fwrite ($fp, $request); while ([email protected] ($FP)) {$result. = @ Fgets ($FP, 1024);} Fclose ($FP); return $result;}} Gets the HTML portion of the URL, removing headerfunction geturlhtml ($url, $cookie =false) {$rowdata = Get_url ($url, $cookie), or 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
function Http_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 = = $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/'); curl_setopt ($ch, Curlopt_ Returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ CH); echo $file _contents;
Original starting: http://www.ido321.com/1297.html
6 Get and POST request sending methods