function
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