php 常用函数获取用户IP、curl请求
获取用户ID
/** * @return array|mixed|string|string[] */ public function getClientIP() { if (@$_SERVER["HTTP_ALI_CDN_REAL_IP"]) { $ip = $_SERVER["HTTP_ALI_CDN_REAL_IP"]; } elseif (@$_SERVER["HTTP_X_FORWARDED_FOR"] ?: false) { $ips = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]); $ip = $ips[0]; } elseif (@$_SERVER["HTTP_CDN_SRC_IP"] ?: false) { $ip = $_SERVER["HTTP_CDN_SRC_IP"]; } elseif (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR']; } $ip = str_replace(['::ffff:', '[', ']'], ['', '', ''], $ip); return $ip; }
curl请求
/** * @param $url * @param $data * @param $header * @param $timeout * @return bool|string */ public function gpCurl($url, $data = '', $header = [], $timeout = 30) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if ($header) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); }else{ curl_setopt($ch, CURLOPT_HEADER, 0); } if($data){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $result = curl_exec($ch); curl_close($ch); return $result; }