使用curl库,使用curl库需要将php.ini中开启curl扩展:
打开
php.ini
文件,找到;extension=php_curl.dll
这一行,把前面的分号去掉.
function curl_http_request($url,$data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FASLE);
//如果$data不为空,则为POST请求
if (!empty($data)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error){
throw new Exception('请求发生错误:' . $error);
}
$resultArr = json_decode($output, true);//将json转为数组格式数据
return $resultArr;
}
本地日志记录方便调试
private function logger($log_content)
{
$max_size = 1000000;
$log_filename = "log.txt";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){
unlink($log_filename);
}
file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content."\r\n", FILE_APPEND);
}