Php:6-type get and POST request Send method

Source: Internet
Author: User
Tags array comments curl header ini key query strlen

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;



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.