ホームページ  >  記事  >  php教程  >  http请求封装

http请求封装

WBOY
WBOYオリジナル
2016-06-07 11:43:431556ブラウズ

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 までご連絡ください。