12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- /**
- * curl 请求
- * @param $url string 请求地址
- * @param $headers json 请求头
- * @param $body json 请求体
- * @return mixed
- */
- function curlRequest($url, $headers = [], $body = [], $method = "GET")
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
- $output = curl_exec($ch);
- curl_close($ch);
- $arr = json_decode($output, true);
- return $arr;
- }
- function http_curl($url, $type = 'get', $res = 'json', $arr = '')
- {
- //1.初始化curl
- $ch = curl_init();
- //2.设置curl的参数
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if ($type == 'post') {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
- }
- //3.采集
- $output = curl_exec($ch);
- //4.关闭
- curl_close($ch);
- if ($res == 'json') {
- return json_decode($output, true);
- }
- return $output;
- }
|