[PHP Learning Tutorial]006. Get Web content (URL contents)

Source: Internet
Author: User
Tags blank page

Introduction: Access to Web content is the basic of our implementation of Web page operation, today, we talk about the basic Request page content of several methods.

We seem to do such a thing every day, open a browser, enter the URL, carriage return, a blank page suddenly has something, it may be Baidu or the like Search page, or a packed with text and pictures of the portal.

We can understand the process from three aspects, one is the browser, two is the server, the third is the browser and server communication between the protocol.

Of course, we don't talk today < Web request process >

this time, let's talk about how to request Web page content in PHP code .

Get Web content methods

1. file_get_contents +[request mode: GET]

<?  $url  = ' http://do.org.cn '$htmlfile_get_contents($ URLecho$html;

2. file_get_contents +[request mode: POST]

No cookie action is required, using the following methods:

<?PHP$url= ' http://do.org.cn/upload.php '; $data=Http_build_query(Array(' foo ' = ' bar ')); $params=Array(    ' http ' =Array(        ' Method ' = ' POST ', ' content ' =$data, ' header ' = ' content-type:application/x-www-form-urlencoded\r\n '. "Content-length:".strlen($data) . "\ r \ n"    ));$context=stream_context_create($params); $html= @file_get_contents($url, ‘‘,$context);

If a cookie is required, add the next line to the ' header ' in $params (similar to this):


3.fopen +[request mode: GET]

<?PHP//try to open a webpage$fp=fopen($url, ' R '); //Get header information$header=Stream_get_meta_data($fp); while(!feof($fp)) {     $result.=fgets($fp, 1024); } fclose($fp); //Output ResultsEcho"URL Header: {$header} <br/> "; Echo"URL Body:$result"; ?>

4 . fopen +[request method: POST]

<?PHP$data=Http_build_query(Array(' foo1 ' = ' bar1 ', ' foo2 ' = ' bar2 ')); $params=Array(     ' http ' =Array(         ' Method ' = ' POST ', ' content ' =$data, ' header ' = ' content-type:application/x-www-form-urlencoded\r\ncookie:cook1=c3;cook2=c4\r\n '. "Content-length:".strlen($data) . "\ r \ n"    )); $context=stream_context_create($params); $fp=fopen(' http://do.org.cn/upload.php ', ' RB ',false,$context); $content=fread($fp, 1024);fclose($fp);Echo $content; ?>

5 . fsockopen+[request mode: GET]

Use the Fsockopen function to open URL URLs to request complete data, including header and body, in get.

<?PHPfunctionGet_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$queryHttp/1.1\r\n "; $request. = "Host:$url[host]\r\n]; $request. = "connection:close\r\n"; if($cookie) {            $request. = "Cookie:$cookie\ r \ n "; }        $request. = "\ r \ n"; fwrite($fp,$request);  while([email protected]feof($fp)) {             $result.= @fgets($fp, 1024); }        fclose($fp); return $result; }}//gets the HTML part of the URL, removing the headerfunctionGet_html ($url,$cookie=false) {    $data= Get_url ($url,$cookie); if($data) {        $body=Stristr($data, "\r\n\r\n"); $body=substr($body, 4,strlen($body)); return $body; }     return false; }?>

6 . fsockopen+[request mode: POST]

Use the Fsockopen function to open URL URLs and post requests for complete data, including header and body.

<?PHPfunctionPost_url ($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 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.1\n "; $request. = "Host:".$url _info["Host"]. " \ n "; $request. = "Referer:$referer\ n "; $request. = "content-type:application/x-www-form-urlencoded\n"; $request. = "Content-length:".strlen($data _string)." \ n "; $request. = "Connection:close\n"; $request. = "Cookie:$cookie\ n "; $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; }?>

7 . Curl Library

Note: Before using the Curl Library, you need to check to see if php.ini has the curl extension turned on.

<?PHP$ch=curl_init ();$timeout= 5; curl_setopt ($ch, Curlopt_url, ' http://do.org.cn/'); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout,$timeout); $content= Curl_exec ($ch); Curl_close ($ch); Echo $content; ?>

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4805187.html

[PHP Learning Tutorial]006. Get Web content (URL contents)

Related Article

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.