In I94web blog, I tried the Chang and talk about two kinds of social comment box, and then abandoned the speech, unsafe.
Whether it's a talk or a lot, I need to crawl the comments from a remote article and store it in the local database. For multiple claims, the format of the request is as follows:
Gets the number of comments, the argument is the article ID
function Getcommcount ($postid)
{
$jsondata = file_get_contents ("http://api.duoshuo.com/threads/counts.json?short_name=i94web&threads= $postid" ); Sets true to return an array without setting or false returns the object $resjson = Json_decode ($jsondata, true); return $resjson [' response '] [$postid] [' comments '];
}
There are a number of ways to remote requests. Today, LZ on the six species, for your reference.
1, with file_get_contents to get way to obtain content:
<?php
$url = ' http://www.ido321.com/';
$html = file_get_contents ($url);
Echo $html;
?>
2, with fopen Open the URL, get 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, using file_get_contents to get content in post way:
$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, including header and Body,fsockopen need php.ini allow_url_fopen option 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, 30);
if (! $fp) {
return false;
} else {
$request = ' Get $query http/1.1rn ';
$request. = ' Host: $url [host]r n ';
$request. = ' Connection:closern ';
if ($cookie) $request. = ' cookie: $cookien ';
$request. = ' RN ';
Fwrite ($fp, $request);
while (! @feof ($fp)) {
$result. = @fgets ($fp, 1024);
}
Fclose ($FP);
return $result;
}
//Get the HTML portion of the URL, remove header
function geturlhtml ($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, Use the Fsockopen function to open the URL, the post way to get the complete data, including header and body
function Http_post ($URL, $data, $cookie, $referrer = ")
{
Parsing the given URL
$URL _info=parse_url ($URL);
Building referrer
if ($referrer = =)//If not given use this script as referrer
$referrer = ' 111 ';
Making string from $data
foreach ($data as $key => $value)
$values []= ' $key = '. UrlEncode ($value);
$data _string=implode (' & ', $values);
Find out which the port is needed–if to 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, Use the Curl Library, before using the Curl Library, you may need to see if php.ini has opened 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;