Home > php教程 > php手册 > http请求封装

http请求封装

WBOY
Release: 2016-06-07 11:43:43
Original
1602 people have browsed it

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元

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template