php curl函数
PHP支持的curl函数库允许你与各种的服务器使用各种类型的协议进行连接和通讯。说白了就是请求接口,PHP中使用cURL实现Get和Post请求的方法,常用的也就是get或者post请求方式。
那么在这我封装了一个方法,把get,post封装了一下,可以用来请求url
/**
* @param $url
* @param string $method
* @param string $data
* @param string $http
* @return mixed
*/
function requests($url, $method = "get", $data = "", $http = "https")
{
//初始化
$ch = curl_init($url);
//设置curl参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($http == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//提交请求
$arr = curl_exec($ch);
curl_close($ch);
return $arr;
}```
## 调用即可用。