function curlQytl($url,$data,$type = 'form',$token=null){ if($type=='form'){ $headers = array( 'Content-Type: application/x-www-form-urlencoded' ); }else{ $headers = array( 'Content-Type: application/json', 'Authorization: ' . $token['token_type'].' '.$token['access_token']// 第三方驗證 ); } if(is_array($data)){ $data = json_encode($data); } $curl = curl_init(); //請求地址 curl_setopt($curl, CURLOPT_URL, $url); //頭部驗證 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //頭部驗證 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //是否返回結果 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //請求超時設置(單位秒,0不做限制) // curl_setopt($curl, CURLOPT_TIMEOUT, 0); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10000); //https請求,(false為https) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //請求方式POST curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); //參數設置 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //執行 $response = curl_exec($curl); //關閉 curl_close($curl); //返回 return $response; }
第一種type='form'參數 $data = 'username=[mrwang]&password=[123456]'; 不用括號