Home >Backend Development >PHP Tutorial >php中curl实现get和post请求

php中curl实现get和post请求

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

php 使用 curl 模拟实现 get 和 post请求的方法。

<p><?php</p>/*<br />* url post请求地址<br />* post post数据<br />* cookie cookie数据,传递一个包含HTTP cookie的头连接<br />* cookie 获取到的cookie信息的保存位置<br />* referer 在HTTP请求中包含一个"referer"头的字符串<br />*/<br />function vcurl($url,$post='',$cookie='',$cookiejar='',$referer=''){ <br />	$tmpInfo='';<br />	$cookiepath=getcwd().'./'.$cookiejar;<br />	$curl=curl_init();<br />	curl_setopt($curl,CURLOPT_URL,$url);<br />	curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);<br />	if($referer){<br />		curl_setopt($curl,CURLOPT_REFERER,$referer);<br />	}else{<br />		curl_setopt($curl,CURLOPT_AUTOREFERER,1);<br />	}<br />	if($post){<br />		curl_setopt($curl,CURLOPT_POST,1);<br />		curl_setopt($curl,CURLOPT_POSTFIELDS,$post);<br />	}<br />	if($cookie){<br />		curl_setopt($curl,CURLOPT_COOKIE,$cookie);<br />	}<br />	if($cookiejar){<br />		curl_setopt($curl,CURLOPT_COOKIEJAR,$cookiepath);<br />		curl_setopt($curl,CURLOPT_COOKIEFILE,$cookiepath);<br />	}<br />	curl_setopt($curl,CURLOPT_TIMEOUT,100);<br />	curl_setopt($curl,CURLOPT_HEADER,0);<br />	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);<br />	$tmpInfo=curl_exec($curl);<br />	if(curl_errno($curl)){<br />		echo '<pre class="brush:php;toolbar:false"><b>错误:</b><br />'.curl_error($curl);<br />	}<br />	curl_close($curl);<br />	return $tmpInfo;<br /><p>}</p>

使用方法如下:

echo vcurl('http://www.scutephp.com/post.php',array('id'=>235,'title'=>'php模拟发送get和post请求'),'',tempnam('./temp','cookie'));


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