A common function for sending HTTP requests via Curl

Source: Internet
Author: User


functionCurl_get ($url,$params) {    returnCurl_http ($url,$params, ' GET ');}functionCurl_post ($url,$params) {    returnCurl_http ($url,$params, ' POST ');}functionCurl_http ($url,$params,$method= ' GET ',$header=Array(),$multi=false){    $opts=Array(Curlopt_timeout= 30,Curlopt_returntransfer= 1,Curlopt_ssl_verifypeer=false,Curlopt_ssl_verifyhost=false,Curlopt_httpheader=$header    ); /*set specific parameters based on request type*/    Switch(Strtoupper($method)){         Case' GET ':$opts[Curlopt_url] =$url. ‘?‘ .Http_build_query($params);  Break;  Case' POST '://determine whether to transfer files            $params=$multi?$params:Http_build_query($params); $opts[Curlopt_url] =$url; $opts[Curlopt_post] = 1; $opts[Curlopt_postfields] =$params;  Break; default:Throw New Exception(' Unsupported request mode! ‘); }    /*initializing and executing a curl request*/    $ch=Curl_init (); Curl_setopt_array ($ch,$opts); $data= Curl_exec ($ch); $error= Curl_error ($ch); Curl_close ($ch); if($error)Throw New Exception(' The request has an error: '.$error); return  $data;}

Reference Address: http://www.thinkphp.cn/topic/23686.html

A common function for sending HTTP requests via Curl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.