In the i94web blog, I tried to chat and talk about two kinds of social comments. later, I gave up talking and was insecure.
In the i94web blog, I tried to chat and talk about two kinds of social comments. later, I gave up talking and was insecure.
Whether talking or speaking, I need to capture the comments of the article remotely and store them in the local database. For more information, the request format is as follows:
// Gets the number of comments. the parameter 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. If no value is set or false, the returned object $ resjson = json_decode ($ jsondata, true) is returned ); return $ resjson ['response'] [$ postid] ['comments'];
}
There are many methods for remote requests. Today, LZ has collected six types for your reference.
1. use file_get_contents to get the content in get mode:
$ Url = 'http: // www.ido321.com /';
$ Html = file_get_contents ($ url );
Echo $ html;
?>
2. open the url with fopen and get it with get
$ 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 get the content in post mode:
$ 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. use the fsockopen function to open the url and get the complete data, including the header and body. fsockopen must be enabled using the allow_url_fopen option in PHP. ini.
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] RN ';
$ Request. = 'connection: closern ';
If ($ cookie) $ request. = 'cookie: $ cookien ';
$ Request. = 'RN ';
Fwrite ($ fp, $ request );
While (! @ Feof ($ fp )){
$ Result. = @ fgets ($ fp, 1024 );
}
Fclose ($ fp );
Return $ result;
}
}
// Obtain the html part of the url and remove the 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 and obtain the complete data in POST mode, including the 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 = '000000 ';
// Making string from $ data
Foreach ($ data as $ key => $ value)
$ Values [] = '$ key ='. urlencode ($ value );
$ Data_string = implode ('&', $ values );
// Find out which port is needed-if not given use standard (= 80)
If (! Isset ($ URL_Info ['port'])
$ URL_Info ['port'] = 80;
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.