PHP send a GET, POST request several methods, getpost_php tutorial

Source: Internet
Author: User

PHP sends a GET, POST request several methods, Getpost


Transferred from: http://blog.csdn.net/haha00217/article/details/7969504

Method 1: Use file_get_contents to get the content in the Get mode

 
 1 
   PHP  2$url= ' http://www.domain.com/';   3 $html file_get_contents ($url);   4 Echo $html ;   5 ?>  

Method 2: Open the URL with fopen and get the content in Get mode

 
 1 
   PHP  2$fpfopen($url, ' R ');   3 //

1 Stream_get_meta_data ($fp);  

1  while (! feof ($fp)) {  2$resultfgets($fp, 1024x768);   3 }  4echo$result";   5 fclose ($fp);   6 ?>  7  

Method 3: Use the file_get_contents function to get the URL by post

 
 1 
   PHP  2$dataarray (' foo ' = ' bar ');  

1 // generate Url-encode Request string, convert array to string   2 $data Http_build_query ($data);   3 $opts Array (    4array  (    5  6   7  Strlen($data). "\ r \ n",   8$data   9)  

1 // generate a handle file  for the request 2 $context stream_context_create ($opts);   3 $html file_get_contents false $context );   4 Echo $html ;   5 ?>  

Method 4: Open the URL with the Fsockopen function, get the complete data in Get mode, including header and Body,fsockopen need php.ini allow_url_fopen option to open

1 
 Php2 functionGet_url ($url,$cookie=false)  3 {  4 $url=Parse_url($url); 5 $query=$url[path]. "?".$url[query]; 6 Echo"Query:".$query; 7 $fp=Fsockopen($url[Host],$url[Port]?$url[Port]:80,$errno,$errstr, 30); 8 if(!$fp) {  9 return false; Ten}Else {   One $request= "GET$queryHttp/1.1\r\n ";  A $request. = "Host:$url[host]\r\n];  - $request. = "connection:close\r\n";  - if($cookie)$request. = "Cookie:$cookie\ n ";  the $request. = "\ r \ n";  - fwrite($fp,$request);  -  while()) {   - $result.= @fgets($fp, 1024);  + }   - fclose($fp);  + return $result;  A }   at }   - //gets the HTML part of the URL, removing the header - functionGeturlhtml ($url,$cookie=false)   - {   - $rowdata= Get_url ($url,$cookie);  - if($rowdata)   in {   - $body=Stristr($rowdata, "\r\n\r\n");  to $body=substr($body, 4,strlen($body));  + return $body;  - }   the     return false;  * }   $?>

Method 5: Use the Fsockopen function to open the URL, post to get the full data, including header and body

1 
 Php2 functionHttp_post ($URL,$data,$cookie,$referrer="")  3 {  4     //parsing the given URL5 $URL _info=Parse_url($URL); 6     //Building Referrer7 if($referrer=="")//if not given use this script as referrer8 $referrer= "111"; 9     //making string from $dataTen foreach($data  as $key=$value)   One $values[]="$key=".UrlEncode($value);  A $data _string=implode("&",$values);  -     //Find out which port was needed-if not given use standard (=80) - if(!isset($URL _info["Port"]))   the $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 ";  A $request. = "Connection:close\n";  at     $request. = "Cookie:$cookie\ n ";  -     $request. = "\ n";  - $request.=$data _string." \ n ";  -     $fp=Fsockopen($URL _info["Host"],$URL _info["Port"]);  - fputs($fp,$request);  -  while(!feof($fp)) {   in $result.=fgets($fp, 1024);  - }   to fclose($fp);  +     return $result;  - }   the?>

Method 6: Using the Curl Library, before using the Curl Library, you may need to see if php.ini has the curl extension turned on

 
 1 
   PHP   2$ch = curl_init ();   3 $timeout = 5;   4 curl_setopt ($ch, Curlopt_url, ' http://www.domain.com/');   5 curl_setopt ($ch, Curlopt_returntransfer, 1);   6 curl_setopt ($ch$timeout);   7 $file _contents = curl_exec ($ch);   8 curl_close ($ch);   9 Echo $file _contents ;   Ten ?>  

http://www.bkjia.com/PHPjc/1130487.html www.bkjia.com true http://www.bkjia.com/PHPjc/1130487.html techarticle PHP sends a GET, POST request several methods, Getpost turn from: http://blog.csdn.net/haha00217/article/details/7969504 Method 1: With file_get_ Contents get content 1 in Get mode? PHP 2 ...

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