public function actions() { $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".self::ip(); $str = self::sendCurl('http://pv.sohu.com/cityjson'); echo '<pre>'; print_r($url);die; $str1= explode('{"cip": "',$str); $str2=(explode('", "cid":',$str1['1'])); $last = $str2['0']; echo $str;die; exit(json_encode(['resCode' => 404, 'resMsg' => '接口错误'])); } public static function sendCurl($url = '', $data = []) { $ch = curl_init(); //设置选项,包括URl curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); //post数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //post的变量 //执行并获取内容 $info = curl_exec($ch); curl_close($ch); return $info; } public static function ip() { if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } $res = preg_match ( '/[d.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; return $res; }