if(!function_exists('CurlHttp')) { function CurlHttp($url,$method='GET',$data='',$header=array(),$timeout=5) { $response = ''; try{ $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //設置請求的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //設爲TRUE把curl_exec()結果轉化爲字串,而不是直接輸出 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//超時設置 $headers = array(); foreach($header as $key=>$value){ $headers[] = $key.' : '. $value; } if(is_array($data)){ $temp = array(); foreach($data as $key=>$value) { $temp[] = $key.'='.$value; } $params = implode('&',$temp); unset($temp); }else{ $params = $data; } curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);//設置HTTP頭信息 switch ($method){ //case "GET" : //curl_setopt($ch, CURLOPT_HTTPGET, true);break; case "POST": curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; case "DELETE": curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; default : curl_setopt($ch, CURLOPT_HTTPGET, true);break; } $response = curl_exec($ch);//執行預約義的CURL $info = curl_getinfo($ch); $errno = curl_errno($ch); if($errno){ throw new Exception(curl_getinfo($ch),$errno); } curl_close($ch); }catch(Exception $e){ $errArr = array( 'curlerrno'=>$e->getCode(), 'curlinfo' =>$e->getMessage() ); Log::errro('curlerror',$errArr); } return $response; } }