標籤:
本文主要展示了php發送get、post請求的6種方法的程式碼範例,分別為使用file_get_contents 、fopen、fsockopen、curl來發送GET和POST請求,代碼如下:
方法1: 用file_get_contents 以get方式擷取內容:
1 <?php2 $url=‘http://www.jb51.net/‘;3 $html = file_get_contents($url);4 echo $html;5 ?>
方法2: 用fopen開啟url, 以get方式擷取內容:
1 <?php2 $fp = fopen($url, ‘r‘);3 stream_get_meta_data($fp);4 while(!feof($fp)) {5 $result .= fgets($fp, 1024);6 }7 echo “url body: $result”;8 fclose($fp);9 ?>
方法3:用file_get_contents函數,以post方式擷取url
1 <?php 2 $data = array (‘foo‘ => ‘bar‘); 3 $data = http_build_query($data); 4 5 $opts = array ( 6 ‘http‘ => array ( 7 ‘method‘ => ‘POST‘, 8 ‘header‘=> “Content-type: application/x-www-form-urlencodedrn” . “Content-Length: ” . strlen($data) . “rn”,10 ‘content‘ => $data11 )12 );13 14 $context = stream_context_create($opts);15 $html = file_get_contents(‘http://localhost/e/admin/test.html‘, false, $context);16 17 echo $html;18 ?>
方法4:用fsockopen函數開啟url,以get方式擷取完整的資料,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 選項開啟
1 <?php 2 function get_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;10 } else {11 $request = “GET $query HTTP/1.1rn”;12 $request .= “Host: $url[host]rn”;13 $request .= “Connection: Closern”;14 if($cookie) $request.=”Cookie: $cookien”;15 $request.=”rn”;16 fwrite($fp,$request);17 while([email protected]feof($fp)) {18 $result .= @fgets($fp, 1024);19 }20 fclose($fp);21 return $result;22 }23 }24 //擷取url的html部分,去掉header25 function GetUrlHTML($url,$cookie=false)26 {27 $rowdata = get_url($url,$cookie);28 if($rowdata)29 {30 $body= stristr($rowdata,”rnrn”);31 $body=substr($body,4,strlen($body));32 return $body;33 }34 35 return false;36 }37 ?>
方法5:用fsockopen函數開啟url,以POST方式擷取完整的資料,包括header和body
1 <?php 2 function HTTP_Post($URL,$data,$cookie, $referrer=”") 3 { 4 5 // parsing the given URL 6 $URL_Info=parse_url($URL); 7 8 // Building referrer 9 if($referrer==”") // if not given use this script as referrer10 $referrer=”111″;11 12 // making string from $data13 foreach($data as $key=>$value)14 $values[]=”$key=”.urlencode($value);15 $data_string=implode(“&”,$values);16 17 // Find out which port is needed – if not given use standard (=80)18 if(!isset($URL_Info["port"]))19 $URL_Info["port"]=80;20 21 // building POST-request:22 $request.=”POST “.$URL_Info["path"].” HTTP/1.1n”;23 $request.=”Host: “.$URL_Info["host"].”n”;24 $request.=”Referer: $referern”;25 $request.=”Content-type: application/x-www-form-urlencodedn”;26 $request.=”Content-length: “.strlen($data_string).”n”;27 $request.=”Connection: closen”;28 29 $request.=”Cookie: $cookien”;30 31 $request.=”n”;32 $request.=$data_string.”n”;33 34 $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);35 fputs($fp, $request);36 while(!feof($fp)) {37 $result .= fgets($fp, 1024);38 }39 fclose($fp);40 41 return $result;42 }43 ?>
方法6:使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經開啟了curl擴充
1 <?php 2 $ch = curl_init(); 3 $timeout = 5; 4 curl_setopt ($ch, CURLOPT_URL, ‘http://www.jb51.net/‘); 5 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 6 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 7 $file_contents = curl_exec($ch); 8 curl_close($ch); 9 10 echo $file_contents;11 ?>
php發送get、post請求的6種方法程式碼範例