http请求封装<?php <br />
/*<br>
* http request tool<br>
*/<br>
/*<br>
* get method<br>
*/<br>
function get($url, $param=array()){<br>
if(!is_array($param)){<br>
throw new Exception("参数必须为array");<br>
}<br>
$p='';<br>
foreach($param as $key => $value){<br>
$p=$p.$key.'='.$value.'&';<br>
}<br>
if(preg_match('/?[dD] /',$url)){//matched ?c<br>
$p='&'.$p;<br>
}else if(preg_match('/?$/',$url)){//matched ?$<br>
$p=$p;<br>
}else{<br>
$p='?'.$p;<br>
}<br>
$p=preg_replace('/&$/','',$p);<br>
$url=$url.$p;<br>
//echo $url;<br>
$httph =curl_init($url);<br>
curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);<br>
curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);<br>
curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);<br>
curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");<br>
<br>
curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);<br>
curl_setopt($httph, CURLOPT_HEADER,1);<br>
$rst=curl_exec($httph);<br>
curl_close($httph);<br>
return $rst;<br>
}<br>
/*<br>
* post method<br>
*/<br>
function post($url, $param=array()){<br>
if(!is_array($param)){<br>
throw new Exception("参数必须为array");<br>
}<br>
$httph =curl_init($url);<br>
curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);<br>
curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);<br>
curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);<br>
curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");<br>
curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式 <br>
curl_setopt($httph, CURLOPT_POSTFIELDS, $param);<br>
curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);<br>
curl_setopt($httph, CURLOPT_HEADER,1);<br>
$rst=curl_exec($httph);<br>
curl_close($httph);<br>
return $rst;<br>
}
http.zip ( 680 B 下载:157 次 )
AD:真正免费,域名 虚机 企业邮箱=0元