<?php /** * php類比curl請求 * * @param string $url 請求的url * @param string $method 請求的方法, 預設POST * @param array $data 請求傳遞的資料 * @param array $header 請求設定的頭資訊 * @param int $head 是否列印頭資訊 * @param int $body 是否列印body資訊 * @param int $timeout 設定逾時時間 * * @return array */ function curl($url,$method="POST",$data=array(),$header=array(),$head=0,$body=0,$timeout = 30) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if (strpos($url, "https") !== false ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if (isset($_SERVER['HTTP_USER_AGENT'])) { curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); } } if (!empty($header)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } switch ($method) { case 'POST': curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); break; case 'GET': break; case 'PUT': curl_setopt($ch, CURLOPT_PUT, 1); curl_setopt($ch, CURLOPT_INFILE, ''); curl_setopt($ch, CURLOPT_INFILESIZE, 10); break; case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; default: break; } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, $head); curl_setopt($ch, CURLOPT_NOBODY, $body); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $rtn = curl_exec($ch); //獲得返回 if (curl_errno($ch)) { echo 'Errno'.curl_error($ch);//捕抓異常 } curl_close($ch); return $rtn; } ?> |