首页  >  文章  >  后端开发  >  php模拟get请求方法总结

php模拟get请求方法总结

WBOY
WBOY原创
2016-06-20 13:04:051887浏览

php 模拟发送 get 请求的几种方法总结。

(1)php 通过 file_get_contents 模拟发送 get 请求

<?php</p>$url='http://www.scutephp.com/';<br />$re=file_get_contents($url);<br /><p>print_r($re);</p>

(2)php 通过 curl 模拟发送 get 请求

<p><?php</p>$ch=curl_init('http://www.scutephp.com/');<br />curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);<br />curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);<br />$output=curl_exec($ch);<br />$fh=fopen("out.html",'w');<br />fwrite($fh,$output);<br /><p>fclose($fh);

(3)php 通过 fsocket 模拟发送 get 请求

<p><?php</p>function sock_get($url){  <br />	$info=parse_url($url);<br />	$fp=fsockopen($info["host"],80,$errno,$errstr,3);<br />	$head="GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";<br />	$head.="Host: ".$info['host']."\r\n";<br />	$head.="\r\n";<br />	$write=fputs($fp,$head);<br />	while(!feof($fp)){  <br />		$line=fgets($fp); <br />		echo $line."<br>";<br />	}<br /><p>}</p>

该函数的使用方法如下:

<p>$url='http://www.scutephp.com/jquery-effects/650.html?page=2';</p>echo "以下是GET方式的响应内容:<br>";<br />sock_get($url);


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn