1<?php 2 /** 3 * 发起一个HTTP(S)请求,并返回json格式的响应数据 4 * @param array 错误信息 array($errorCode, $errorMessage) 5 * @param string 请求Url 6 * @param array 请求参数 7 * @param string 请求类型(GET|POST) 8 * @param int 超时时间 9 * @param array 额外配置 10 * 11 * @return array 12 */ 13 public function curl_request_json(&$error, $url, $param = array(), $method = 'GET', $timeout = 10, $exOptions = null) { 14 $error = false; 15 $responseText = curl_request_text($error, $url, $param, $method, $timeout, $exOptions); 16 $response = null; 17 if ($error == false && $responseText > 0) { 18 $response = json_decode($responseText, true); 19 20 if ($response == null) { 21 $error = array('errorCode'=>-1, 'errorMessage'=>'json decode fail', 'responseText'=>$responseText); 22 //将错误信息记录日志文件里 23 $logText = "json decode fail : $url"; 24 if (!empty($param)) { 25 $logText .= ", param=".json_encode($param); 26 } 27 $logText .= ", responseText=$responseText"; 28 file_put_contents("/data/error.log", $logText); 29 } 30 } 31 return $response; 32 } 33 34 /** 35 * 发起一个HTTP(S)请求,并返回响应文本 36 * @param array 错误信息 array($errorCode, $errorMessage) 37 * @param string 请求Url 38 * @param array 请求参数 39 * @param string 请求类型(GET|POST) 40 * @param int 超时时间 41 * @param array 额外配置 42 * 43 * @return string 44 */ 45 public function curl_request_text(&$error, $url, $param = array(), $method = 'GET', $timeout = 15, $exOptions = NULL) { 46 //判断是否开启了curl扩展 47 if (!function_exists('curl_init')) exit('please open this curl extension'); 48 49 //将请求方法变大写 50 $method = strtoupper($method); 51 52 $ch = curl_init(); 53 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 54 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 55 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 56 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 57 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 58 curl_setopt($ch, CURLOPT_HEADER, false); 59 if (isset($_SERVER['HTTP_USER_AGENT'])) curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 60 if (isset($_SERVER['HTTP_REFERER'])) curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']); 61 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 62 switch ($method) { 63 case 'POST': 64 curl_setopt($ch, CURLOPT_POST, true); 65 if (!empty($param)) { 66 curl_setopt($ch, CURLOPT_POSTFIELDS, (is_array($param)) ? http_build_query($param) : $param); 67 } 68 break; 69 70 case 'GET': 71 case 'DELETE': 72 if ($method == 'DELETE') { 73 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 74 } 75 if (!empty($param)) { 76 $url = $url.(strpos($url, '?') ? '&' : '?').(is_array($param) ? http_build_query($param) : $param); 77 } 78 break; 79 } 80 curl_setopt($ch, CURLINFO_HEADER_OUT, true); 81 curl_setopt($ch, CURLOPT_URL, $url); 82 //设置额外配置 83 if (!empty($exOptions)) { 84 foreach ($exOptions as $k => $v) { 85 curl_setopt($ch, $k, $v); 86 } 87 } 88 $response = curl_exec($ch); 89 90 $error = false; 91 //看是否有报错 92 $errorCode = curl_errno($ch); 93 if ($errorCode) { 94 $errorMessage = curl_error($ch); 95 $error = array('errorCode'=>$errorCode, 'errorMessage'=>$errorMessage); 96 //将报错写入日志文件里 97 $logText = "$method $url: [$errorCode]$errorMessage"; 98 if (!empty($param)) $logText .= ",$param".json_encode($param); 99 file_put_contents('/data/error.log', $logText); 100 } 101 102 curl_close($ch); 103 104 return $response; 105 106 107 108 } 109 110 111 112 113 114 115 116 117 118 119 120 121 122?> 123 124获取用户信息的自定义函数
CURL请求
Wesley13
2021-10-11
1302 0 0
点赞
收藏
评论区
加载中...