{"id":10008242,"date":"2023-09-07T20:57:58","date_gmt":"2023-09-07T12:57:58","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008242"},"modified":"2023-09-07T20:57:58","modified_gmt":"2023-09-07T12:57:58","slug":"php%e8%8e%b7%e5%8f%96%e5%ae%a2%e6%88%b7%e7%ab%afip%e5%9c%b0%e5%9d%80%e5%8f%8aip%e6%89%80%e5%9c%a8%e5%9b%bd%e5%ae%b6%e3%80%81%e7%9c%81%e4%bb%bd%e3%80%81%e5%9f%8e%e5%b8%82%e3%80%81%e5%8e%bf%e5%8c%ba","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008242","title":{"rendered":"php\u83b7\u53d6\u5ba2\u6237\u7aefip\u5730\u5740\u53caip\u6240\u5728\u56fd\u5bb6\u3001\u7701\u4efd\u3001\u57ce\u5e02\u3001\u53bf\u533a"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u83b7\u53d6\u5ba2\u6237\u7aefip\u5730\u5740\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e2aip\u5730\u5740\u83b7\u53d6\u6240\u5728\u7684\u56fd\u5bb6\u3001\u7701\u4efd\u3001\u57ce\u5e02\uff0c\u53ef\u4ee5\u5728\u7f51\u7ad9\u4e2d\u5b9e\u73b0IP\u5c5e\u5730\uff0c\u53d1\u5e03\u5730\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u672c\u6587\u7684\u83b7\u53d6IP\u5730\u5740\u4fe1\u606f\u5747\u91c7\u81ea\u7f51\u7edc\u4e0a\u514d\u8d39\u7684IP\u67e5\u8be2\u7f51\u7ad9\uff0c\u901a\u8fc7\u5176API\u6216\u8005\u7f51\u9875HTML\u89e3\u6790\u51fa\u7684ip\u5730\u5740\u4fe1\u606f\u3002<\/p>\n<h2>\u4ee3\u7801<\/h2>\n<pre><code>&lt;?php\n\n    \/\/ \u7f16\u7801\n    header('Content-type:application\/json');\n    \n    $ip = $_GET['ip'];\n    \n    \/\/ \u8fc7\u6ee4\u7a7a\u6570\u636e\n    if(!$ip) {\n        \n        $ipinfo = array(\n            'code' =&gt; 201,\n            'msg' =&gt; '\u672a\u4f20\u5165ip\u5730\u5740'\n        );\n        echo json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n        exit;\n    }\n    \n    \/\/ \u9a8c\u8bc1ipv4\u5730\u5740\u5408\u6cd5\u6027\n    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {\n        \n        $ipinfo = array(\n            'code' =&gt; 201,\n            'msg' =&gt; '\u8fd9\u4e0d\u662f\u4e00\u4e2a\u6b63\u786e\u7684ip\u5730\u5740'\n        );\n        echo json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n        exit;\n    }\n    \n    \/\/ \u8bf7\u6c42\u63a5\u53e3\n    $methods = [\n        'getMethod_1',\n        'getMethod_2',\n        'getMethod_3',\n        'getMethod_4',\n        'getMethod_5'\n    ];\n    \n    foreach ($methods as $method) {\n        $response = json_decode($method($ip));\n        if ($response-&gt;code === 200) {\n            \n            \/\/ \u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u8f93\u51fa\u8bf7\u6c42\u7ed3\u679c\u5e76\u505c\u6b62\u5faa\u73af\n            echo $method($ip);\n            break;\n        }\n    }\n    \n    if (!isset($response) || $response-&gt;code !== 200) {\n        \n        $ipinfo = array(\n            'code' =&gt; 201,\n            'msg' =&gt; '\u8bf7\u6c42\u5931\u8d25~'\n        );\n        echo json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n        exit;\n    }\n    \n    \/\/ HTTP\u8bf7\u6c42\u5c01\u88c5\n    function cUrlGetIP($url) {\n        \n        \/\/ cUrl\n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, $url);\n        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\n        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        $header[] = 'user-agent: Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/97.0.4692.99 Safari\/537.36';\n        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n        return curl_exec($ch);\n        curl_close($ch);\n    }\n    \n    \/\/ \u4e2d\u56fd34\u4e2a\u7701\u7ea7\u884c\u653f\u533a\u57df\n    $provinces = array(\n        &quot;\u5317\u4eac&quot;,\n        &quot;\u5929\u6d25&quot;,\n        &quot;\u6cb3\u5317&quot;,\n        &quot;\u5c71\u897f&quot;,\n        &quot;\u5185\u8499\u53e4&quot;,\n        &quot;\u8fbd\u5b81&quot;,\n        &quot;\u5409\u6797&quot;,\n        &quot;\u9ed1\u9f99\u6c5f&quot;,\n        &quot;\u4e0a\u6d77&quot;,\n        &quot;\u6c5f\u82cf&quot;,\n        &quot;\u6d59\u6c5f&quot;,\n        &quot;\u5b89\u5fbd&quot;,\n        &quot;\u798f\u5efa&quot;,\n        &quot;\u6c5f\u897f&quot;,\n        &quot;\u5c71\u4e1c&quot;,\n        &quot;\u6cb3\u5357&quot;,\n        &quot;\u6e56\u5317&quot;,\n        &quot;\u6e56\u5357&quot;,\n        &quot;\u5e7f\u4e1c&quot;,\n        &quot;\u5e7f\u897f&quot;,\n        &quot;\u6d77\u5357&quot;,\n        &quot;\u91cd\u5e86&quot;,\n        &quot;\u56db\u5ddd&quot;,\n        &quot;\u8d35\u5dde&quot;,\n        &quot;\u4e91\u5357&quot;,\n        &quot;\u897f\u85cf&quot;,\n        &quot;\u9655\u897f&quot;,\n        &quot;\u7518\u8083&quot;,\n        &quot;\u9752\u6d77&quot;,\n        &quot;\u5b81\u590f&quot;,\n        &quot;\u65b0\u7586&quot;,\n        &quot;\u9999\u6e2f&quot;,\n        &quot;\u6fb3\u95e8&quot;,\n        &quot;\u53f0\u6e7e&quot;\n    );\n    \n    \/\/ \u63a5\u53e31\n    \/\/ http:\/\/ipshudi.com\/{ip}.htm\n    function getMethod_1($ip) {\n        \n        $response = file_get_contents('http:\/\/ipshudi.com\/'.$ip.'.htm');\n        $str1 = substr($response, strripos($response, &quot;\u5f52\u5c5e\u5730&quot;));\n        $str2 = substr($str1, 0, strrpos($str1, &quot;\u8fd0\u8425\u5546&quot;));\n        $str3 = substr($str2, strripos($str2, &quot;&lt;span&gt;&quot;) + 6);\n        $str4 = substr($str3, 0, strripos($str3, &quot;&lt;\/span&gt;&quot;) + 6);\n        \n        \/\/ \u63d0\u53d6\u56fd\u5bb6\n        $country = substr($str4, 0, strpos($str4, ' '));\n        \n        \/\/ \u63d0\u53d6\u7701\u4efd\n        $str5 = substr($str4, 0, strrpos($str4, &quot; &lt;a href&quot;));\n        $province = substr($str5, strpos($str5, ' ') + 1);\n        \n        \/\/ \u63d0\u53d6\u57ce\u5e02\n        preg_match('\/&gt;([^&lt;]+)&lt;\/', $str4, $matches);\n        $city = $matches[1];\n        \n        \/\/ \u63d0\u53d6\u53bf\u533a\n        $str6 = substr($str4, strripos($str4, &quot;&lt;\/a&gt;&quot;));\n        $district = preg_replace('\/[^\/x{4e00}-\/x{9fa5}]+\/u', '', $str6);\n        \n        \/\/ \u5224\u65ad\u662f\u5426\u83b7\u53d6\u6210\u529f\n        if($country || $province || $city || $district) {\n            \n            \/\/ \u62fc\u63a5\u6570\u7ec4\n            $ipinfo = array(\n                'code' =&gt; 200,\n                'msg' =&gt; '\u83b7\u53d6\u6210\u529f',\n                'ipinfo' =&gt; array(\n                    'country' =&gt; $country,\n                    'province' =&gt; $province,\n                    'city' =&gt; $city,\n                    'district' =&gt; $district,\n                    'ip' =&gt; $ip\n                )\n            );\n        }else {\n            \n            $ipinfo = array(\n                'code' =&gt; 201,\n                'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n            );\n        }\n        \n        return json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n    }\n    \n    \/\/ \u63a5\u53e32\n    \/\/ https:\/\/searchplugin.csdn.net\/api\/v1\/ip\/get?ip={ip}\n    function getMethod_2($ip) {\n        \n        $response = cUrlGetIP('https:\/\/searchplugin.csdn.net\/api\/v1\/ip\/get?ip='.$ip);\n        $code = json_decode($response,true)['code'];\n        \n        if($code == 200) {\n            \n            $str1 = json_decode($response,true)['data']['address'];\n            \n            \/\/ \u56fd\u5bb6\n            $country = explode(' ', $str1)[0];\n            \n            \/\/ \u7701\u4efd\n            $province = explode(' ', $str1)[1];\n            \n            \/\/ \u57ce\u5e02\n            $city = explode(' ', $str1)[2];\n            \n            \/\/ \u53bf\u533a\n            $district = '';\n            \n            \/\/ \u5224\u65ad\u662f\u5426\u83b7\u53d6\u6210\u529f\n            if($country || $province || $city || $district) {\n                \n                \/\/ \u62fc\u63a5\u6570\u7ec4\n                $ipinfo = array(\n                    'code' =&gt; 200,\n                    'msg' =&gt; '\u83b7\u53d6\u6210\u529f',\n                    'ipinfo' =&gt; array(\n                        'country' =&gt; $country,\n                        'province' =&gt; $province,\n                        'city' =&gt; $city,\n                        'district' =&gt; $district,\n                        'ip' =&gt; json_decode($response,true)['data']['ip']\n                    )\n                );\n            }else {\n                \n                $ipinfo = array(\n                    'code' =&gt; 201,\n                    'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n                );\n            }\n        }else {\n            \n            $ipinfo = array(\n                'code' =&gt; 201,\n                'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n            );\n        }\n        \n        return json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n    }\n    \n    \/\/ \u63a5\u53e33\n    \/\/ https:\/\/ipchaxun.com\/{ip}\/\n    function getMethod_3($ip) {\n        \n        $response = cUrlGetIP('https:\/\/ipchaxun.com\/'.$ip.'\/');\n        $str1 = substr($response, strripos($response, &quot;\u5f52\u5c5e\u5730&quot;) + 15);\n        $str2 = substr($str1, 0, strrpos($str1, &quot;\u8fd0\u8425\u5546&quot;));\n        \n        \/\/ \u63d0\u53d6\u7701\u4efd\n        global $provinces;\n        foreach ($provinces as $province_) {\n            if (strpos($str2, $province_) !== false) {\n                $province = $province_;\n                break;\n            }\n        }\n        \n        \/\/ \u63d0\u53d6\u56fd\u5bb6\n        $str3 = substr($str2, 0, strrpos($str2, $province));\n        $country = preg_replace('\/[^\/x{4e00}-\/x{9fa5}]+\/u', '', $str3);\n        \n        \/\/ \u63d0\u53d6\u57ce\u5e02\n        $str4 = substr($str2, strripos($str2, &quot;nofollow&quot;) + 10);\n        $city = substr($str4, 0, strrpos($str4, &quot;&lt;\/a&gt;&quot;));\n        \n        \/\/ \u63d0\u53d6\u53bf\u533a\n        $str6 = substr($str2, strripos($str2, &quot;&lt;\/a&gt;&quot;) + 4);\n        $district = substr($str6, 0, strrpos($str6, &quot;&lt;\/span&gt;&quot;));\n        \n        \/\/ \u5224\u65ad\u662f\u5426\u83b7\u53d6\u6210\u529f\n        if($country || $province || $city || $district) {\n            \n            \/\/ \u62fc\u63a5\u6570\u7ec4\n            $ipinfo = array(\n                'code' =&gt; 200,\n                'msg' =&gt; '\u83b7\u53d6\u6210\u529f',\n                'ipinfo' =&gt; array(\n                    'country' =&gt; $country,\n                    'province' =&gt; $province,\n                    'city' =&gt; $city,\n                    'district' =&gt; $district,\n                    'ip' =&gt; $ip\n                )\n            );\n        }else {\n            \n            $ipinfo = array(\n                'code' =&gt; 201,\n                'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n            );\n        }\n        \n        return json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n    }\n    \n    \/\/ \u63a5\u53e34\n    \/\/ https:\/\/api.vvhan.com\/api\/getIpInfo?ip={ip}\n    function getMethod_4($ip) {\n        \n        $response = cUrlGetIP('https:\/\/api.vvhan.com\/api\/getIpInfo?ip='.$ip);\n        $success = json_decode($response,true)['success'];\n        \n        if($success == true) {\n            \n            $str1 = json_decode($response,true)['info'];\n            \n            \/\/ \u56fd\u5bb6\n            $country = $str1['country'];\n            \n            \/\/ \u7701\u4efd\n            $province = $str1['prov'];\n            \n            \/\/ \u57ce\u5e02\n            $city = $str1['city'];\n            \n            \/\/ \u53bf\u533a\n            $district = '';\n            \n            \/\/ \u5224\u65ad\u662f\u5426\u83b7\u53d6\u6210\u529f\n            if($country || $province || $city || $district) {\n                \n                \/\/ \u62fc\u63a5\u6570\u7ec4\n                $ipinfo = array(\n                    'code' =&gt; 200,\n                    'msg' =&gt; '\u83b7\u53d6\u6210\u529f',\n                    'ipinfo' =&gt; array(\n                        'country' =&gt; $country,\n                        'province' =&gt; $province,\n                        'city' =&gt; $city,\n                        'district' =&gt; $district,\n                        'ip' =&gt; $ip\n                    )\n                );\n            }else {\n                \n                $ipinfo = array(\n                    'code' =&gt; 201,\n                    'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n                );\n            }\n        }else {\n            \n            $ipinfo = array(\n                'code' =&gt; 201,\n                'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n            );\n        }\n        \n        return json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n    }\n    \n    \/\/ \u63a5\u53e35\n    \/\/ https:\/\/c.runoob.com\/wp-content\/themes\/toolrunoob2\/option\/ajax.php?type=checkIP&amp;REMOTE_ADDR={ip}\n    function getMethod_5($ip) {\n        \n        $response = cUrlGetIP('https:\/\/c.runoob.com\/wp-content\/themes\/toolrunoob2\/option\/ajax.php?type=checkIP&amp;REMOTE_ADDR='.$ip);\n\n        $flag = json_decode($response,true)['flag'];\n        \n        if($flag == true) {\n            \n            $str1 = json_decode($response,true)['data'];\n            \n            \/\/ \u56fd\u5bb6\n            $country = $str1['country'];\n            \n            \/\/ \u7701\u4efd\n            $province = $str1['regionName'];\n            \n            \/\/ \u57ce\u5e02\n            $city = $str1['city'];\n            \n            \/\/ \u53bf\u533a\n            $district = '';\n            \n            \/\/ \u5224\u65ad\u662f\u5426\u83b7\u53d6\u6210\u529f\n            if($country || $province || $city || $district) {\n                \n                \/\/ \u62fc\u63a5\u6570\u7ec4\n                $ipinfo = array(\n                    'code' =&gt; 200,\n                    'msg' =&gt; '\u83b7\u53d6\u6210\u529f',\n                    'ipinfo' =&gt; array(\n                        'country' =&gt; $country,\n                        'province' =&gt; $province,\n                        'city' =&gt; $city,\n                        'district' =&gt; $district,\n                        'ip' =&gt; $ip\n                    )\n                );\n            }else {\n                \n                $ipinfo = array(\n                    'code' =&gt; 201,\n                    'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n                );\n            }\n        }else {\n            \n            $ipinfo = array(\n                'code' =&gt; 201,\n                'msg' =&gt; '\u83b7\u53d6\u5931\u8d25'\n            );\n        }\n        \n        return json_encode($ipinfo,JSON_UNESCAPED_UNICODE);\n    }\n\n?&gt;<\/code><\/pre>\n<h2>\u4f7f\u7528<\/h2>\n<p><code>https:\/\/\u57df\u540d\/getipInfo.php?ip=\u9700\u8981\u67e5\u8be2\u7684IP\u5730\u5740<\/code><\/p>\n<p>\u4e00\u5171\u67095\u4e2a\u63a5\u53e3\uff0c\u63a5\u53e3\u5931\u6548\u4f1a\u4f7f\u7528\u4e0b\u4e00\u4e2a\u63a5\u53e3\uff0c\u76f4\u5230\u63a5\u53e3\u6210\u529f\uff0c\u6700\u7ec8\u8f93\u51fa\u6570\u636e\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>{&quot;code&quot;:200,&quot;msg&quot;:&quot;\u83b7\u53d6\u6210\u529f&quot;,&quot;ipinfo&quot;:{&quot;country&quot;:&quot;\u4e2d\u56fd&quot;,&quot;province&quot;:&quot;\u5e7f\u4e1c\u7701&quot;,&quot;city&quot;:&quot;\u5e7f\u5dde\u5e02&quot;,&quot;district&quot;:&quot;\u756a\u79ba\u533a&quot;,&quot;ip&quot;:&quot;xx.xx.xxx.xx&quot;}}<\/code><\/pre>\n<h2>\u4f5c\u8005<\/h2>\n<p>TANKING<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044193205\">https:\/\/segmentfault.com\/a\/1190000044193205<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u83b7\u53d6\u5ba2\u6237\u7aefip\u5730\u5740\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e2aip\u5730\u5740\u83b7\u53d6\u6240\u5728\u7684\u56fd\u5bb6\u3001\u7701\u4efd\u3001\u57ce\u5e02\uff0c\u53ef\u4ee5\u5728\u7f51\u7ad9\u4e2d\u5b9e\u73b0IP\u5c5e\u5730\uff0c\u53d1\u5e03\u5730\u7b49\u529f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008242","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008242"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008242\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}