php发送get、post请求的6种方法代码示例
本文主要展示了php发送get、post请求的6种方法的代码示例,分别为使用file_get_contents 、fopen、fsockopen、curl来发送GET和POST请求,代码如下:
方法1: 用file_get_contents 以get方式获取内容:
1 <?php 2 $url='http://www.jb51.net/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?>
方法2: 用fopen打开url, 以get方式获取内容:
1 <?php 2 $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' => $data 11 ) 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(!@feof($fp)) { 18 $result .= @fgets($fp, 1024); 19 } 20 fclose($fp); 21 return $result; 22 } 23 } 24 //获取url的html部分,去掉header 25 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 referrer 10 $referrer=”111″; 11 12 // making string from $data 13 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 ?>