Home  >  Article  >  Backend Development  >  php模拟get请求方法总结

php模拟get请求方法总结

WBOY
WBOYOriginal
2016-06-20 13:04:051887browse

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);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn