PHP sends a GET, POST request to get the content of several methods

Source: Internet
Author: User
Tags array to string

Method 1:get content with file_get_contents in get mode<?PHP$url= '';$html=file_get_contents($url);Echo $html;?>Method 2: Open the URL with fopen,get content in Get mode<?PHP$fp=fopen($url, ' R '); //Return request flow information (array: Request status, block, return value is empty, return value HTTP first)Stream_get_meta_data($fp);  while(!feof($fp)) {  $result.=fgets($fp, 1024); }  Echo"URL Body:$result"; fclose($fp); ?>Method 3: Use the File_get_contents function,get the URL by post<?PHP$data=Array(' foo ' = ' bar ');//generate Url-encode Request string, convert array to string$data=Http_build_query($data); $opts=Array (  <span style= "White-space:pre" > </span> ' http ' =Array (  <span style= "White-space:pre" > </span> ' method ' = ' POST ', <span style= ' White-space:pre ' > &  Lt;/span> ' header ' = ' content-type:application/x-www-form-urlencoded\r\n '. <span style= "White-space:pre" > </span> "content-length:".strlen($data) . "\ r \ n", <span style= "White-space:pre" > </span> ' content ' =$data<span style= "White-space:pre" > </span>)  );//generate a handle file for the request$context=stream_context_create($opts); $html=file_get_contents(' http://localhost/e/admin/test.html ',false,$context); Echo $html; ?>Method 4: Open the URL with the Fsockopen function and get the full data in Get mode, including header and body, Fsockopen needs PHP.INI in the allow_url_fopen option is turned on<?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\ n "; $request. = "\ r \ n"; fwrite($fp,$request);  while()) {      $result.= @fgets($fp, 1024); }      fclose($fp); return $result; }      }      //gets the HTML part of the URL, removing the header    functionGeturlhtml ($url,$cookie=false)      {      $rowdata= Get_url ($url,$cookie); if($rowdata)      {      $body=Stristr($rowdata, "\r\n\r\n"); $body=substr($body, 4,strlen($body)); return $body; }          return false; }      ?>Method 5: Use the Fsockopen function to open the URL, post to get the full data, including header and body<?PHPfunctionHttp_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 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; }      ?>Method 6: Using the Curl Library, you may need to look at PHP before using the Curl Library.INI extension is already turned on<?PHP$ch=Curl_init (); $timeout= 5; curl_setopt ($ch, Curlopt_url, ''); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout,$timeout); $file _contents= Curl_exec ($ch); Curl_close ($ch); Echo $file _contents; ?>

PHP sends a GET, POST request to get the content of several methods

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: 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.