首頁 >php教程 >php手册 >http请求封装

http请求封装

WBOY
WBOY原創
2016-06-07 11:43:431612瀏覽

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('/\?[\d\D]+/',$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元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn