Heim  >  Artikel  >  php教程  >  http请求封装

http请求封装

WBOY
WBOYOriginal
2016-06-07 11:43:431556Durchsuche

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元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn