Browse Source

ip得到地址改用阿里云的

lizhen_gitee 1 year ago
parent
commit
e9b0d9bf73

+ 36 - 1
application/common.php

@@ -677,7 +677,7 @@ if (!function_exists('curl_get')) {
     /**
      * 发起HTTP GET请求
      */
-    function curl_get($url)
+    function curl_get($url,$header = '')
     {
         $oCurl = curl_init();
         if (stripos($url, "https://") !== FALSE) {
@@ -688,6 +688,10 @@ if (!function_exists('curl_get')) {
         curl_setopt($oCurl, CURLOPT_TIMEOUT, 3);
         curl_setopt($oCurl, CURLOPT_URL, $url);
         curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
+        if($header){
+            curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);
+        }
+        curl_setopt($oCurl, CURLOPT_HEADER, 0);
         $sContent = curl_exec($oCurl);
         $aStatus  = curl_getinfo($oCurl);
         $error    = curl_error($oCurl);
@@ -751,6 +755,37 @@ if (!function_exists('last_week')) {
     }
 }
 
+//阿里天眼数聚,ip地址获取实际地址
+//https://market.aliyun.com/products/57002002/cmapi00052499.html
+function newip_to_address(){
+    $ip = request()->ip();
+    //$ip = '112.6.63.60';
+    $ipaddress = '火星';
+
+    $url = 'https://ipcity.market.alicloudapi.com/ip/city/query?coordsys=coordsys&ip='.$ip;
+
+    $appcode = '3f4fc2408a0143509e5be5c20eb97c5b';
+    $headers = [];
+    array_push($headers, "Authorization:APPCODE " . $appcode);
+
+    $result = json_decode(curl_get($url,$headers),true);
+
+    //dump($result);
+    if(is_array($result) && !empty($result)){
+        if(isset($result['code']) && $result['code'] == 200 ){
+            if(isset($result['data']['result']) && is_array($result['data']['result']) && !empty($result['data']['result'])){
+                $rs = $result['data']['result'];
+
+                if(isset($rs['prov']) && !empty($rs['prov'])){
+                    $ipaddress = $rs['prov'];
+                };
+
+            }
+        }
+    }
+    return $ipaddress;
+}
+
 //ip138接口,ip地址获取实际地址
 function ip_to_address(){
     $ip = request()->ip();

+ 1 - 1
application/common/library/Auth.php

@@ -376,7 +376,7 @@ class Auth
                     Token::clear($user->id);
                 }
 
-                $user->ipaddress = ip_to_address();
+                $user->ipaddress = newip_to_address();
 
                 $ip = request()->ip();
                 $time = time();

+ 4 - 0
application/index/controller/Test.php

@@ -21,6 +21,10 @@ class Test extends Controller
         echo $ipaddress;
     }
 
+    public function newip(){
+        $ipaddress = newip_to_address();
+        echo $ipaddress;
+    }
 
 
     public function im(){