之前封裝的http_curl方法,經(jīng)過后續(xù)使用發(fā)現(xiàn)不夠完善,所以現(xiàn)在把修改好的記錄下來
//封裝request curl方法
public function request($url,$https=true,$method='get',$data=null){
//1.初始化url
$ch = curl_init($url);
//2.設(shè)置請求參數(shù)
//把數(shù)據(jù)以文件流形式保存,而不是直接輸出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//支持http和https協(xié)議
//https協(xié)議 ssl證書
//繞過證書驗(yàn)證
if($https === true){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//支持post請求
if($method === 'post'){
curl_setopt($ch, CURLOPT_POST, true);
//發(fā)送的post數(shù)據(jù)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//3.發(fā)送請求
$content = curl_exec($ch);
//4.關(guān)閉請求
curl_close($ch);
return $content;
}
返回的數(shù)據(jù)同樣需要json_decode();