微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php发送get、post请求的几种方法

 

方法1: 用file_get_contents 以get方式获取内容

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

 

方法2: 用fopen打开url,以get方式获取内容

$fp = fopen($url,'r'3 //返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等) 

 

1 stream_get_Meta_data($fp);  

 

while(!feof($fp)) {  
$result .= fgets($fp,1024}  
echo "url body: $result"5 fclose(6 ?>  
7  

 

方法3:用file_get_contents函数,以post方式获取url

$data = array ('foo' => 'bar');  

 

 1 生成url-encode后的请求字符串,将数组转换为字符串  
 2 http_build_query($data 3 $opts = array (  
 4 <span style="white-space:pre">  </span>'http' =>  5 <span style="white-space:pre">      </span>'method' => 'POST', 6 <span style="white-space:pre">      </span>'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .  
 7 <span style="white-space:pre">      </span>"Content-Length: " . strlen($data) . "\r\n",1)"> 8 <span style="white-space:pre">      </span>'content' => $data  
 9 <span style="white-space:pre">  </span>)  
10 ); 

 

生成请求的句柄文件  
$context = stream_context_create($optsfile_get_contents('http://localhost/e/admin/test.html',1)">false,$context5 ?>  

 

方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启

 1 <? 2 function get_url ($cookie=false 3 {  
 4 $url = parse_url( 5 $query = $url[path]."?".[query];  
 6 echo "Query:".$query 7 fsockopen( $url[host],1)">$url[port]?$url[port]:80,1)">$errno,1)">$errstr,30 8 if (!) {  
 9 return 10 } else {  
11 $request = "GET $query HTTP/1.1\r\n"12 $request .= "Host: $url[host]\r\n"13 $request .= "Connection: Close\r\n"14 if($cookie) $request.="Cookie:   $cookie\n"15 $request.="\r\n"16 fwrite($request17 while()) {  
18 $result .= @19 20 21 return $result22 23 24 获取url的html部分,去掉header  
25 function GetUrlHTML(26 27 $rowdata = get_url($cookie28 $rowdata29 30 $body= stristr($rowdata,"\r\n\r\n"31 $body=substr($body,4,1)">$body));  
32 33 34     35 36 ?>  

  

方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

function HTTP_Post($URL,1)">$data,1)">$cookie,1)">$referrer="" 4      parsing the given URL  
$URL_Info=$URL 6      Building referrer  
 7 $referrer=="")  if not given use this script as referrer  
 8 $referrer="111" 9      making string from $data  
10 foreach($data as $key=>$value$values[]="$key=".urlencode($data_string=implode("&",1)">$values13      Find out which port is needed - if not given use standard (=80)  
if(!isset($URL_Info["port"]))  
$URL_Info["port"]=8016      building POST-request:  
17 $request.="POST ".$URL_Info["path"]." HTTP/1.1\n"$request.="Host: ".$URL_Info["host"]."\n"19 $request.="Referer: $referer\n"20 $request.="Content-type: application/x-www-form-urlencoded\n"21 $request.="Content-length: ".$data_string)."\n"22 $request.="Connection: close\n"23     24     $request.="\n"25 $request.=$data_string."\n"26     fsockopen($URL_Info["host"],1)">]);  
27 fputs(29 30 31 32     34 ?>  

 

方法6:使用curl库,使用curl库之前,可能需要查看一下PHP.ini是否已经打开了curl扩展

$ch = curl_init();  
$timeout = 5 4 curl_setopt ($ch,CURLOPT_URL,'http://www.domain.com/' 5 curl_setopt ( 6 curl_setopt ($timeout$file_contents = curl_exec($ch 8 curl_close($file_contents10 ?>  

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐