{"id":164,"date":"2023-02-18T21:33:38","date_gmt":"2023-02-18T13:33:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=164"},"modified":"2023-02-18T21:34:03","modified_gmt":"2023-02-18T13:34:03","slug":"php-online-ip-address-query","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/php-online-ip-address-query\/","title":{"rendered":"PHP\u5728\u7ebfIP\u5730\u5740\u67e5\u8be2"},"content":{"rendered":"<p>\u4ee5<code>ip.cn<\/code>\u4e3a\u4f8b\uff0c\u6839\u636eIP\u5730\u5740\u5728\u7ebf\u67e5\u8be2\u7701\u5e02\u533a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">&lt;?php\n$url=&#039;https:\/\/ip.cn\/index.php?ip=8.8.8.8&#039;;\n\n$data[&quot;country&quot;] = &#039;unknown&#039;;\n$data[&quot;region&quot;] = &#039;unknown&#039;;\n$data[&quot;city&quot;] = &#039;unknown&#039;;\n\n$ch = curl_init();\ncurl_setopt($ch, CURLOPT_USERAGENT ,&quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/53.0.2785.113 Safari\/537.36&quot;);\ncurl_setopt($ch,CURLOPT_URL,$url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n\n$html = curl_exec($ch);\n\nif (preg_match_all(&#039;%&lt;p.*?&gt;(.*?)&lt;\/p&gt;%si&#039;, $html, $matches)) {\n    $arr[0][] = $matches[1];\n}\nif ($arr[0][0][2] &amp;&amp; strpos($arr[0][0][2], &#039;GeoIP&#039;) !== FALSE) {\n    $geoip = str_replace(&quot;GeoIP:&quot;, &quot;&quot;, $arr[0][0][2]);\n    $geo_arr = explode(&quot;,&quot;, $geoip);\n    if (count($geo_arr) == 3) {\n        $data[&#039;country&#039;] = trim($geo_arr[2]);\n        $data[&#039;region&#039;] = trim($geo_arr[1]);\n        $data[&#039;city&#039;] = trim($geo_arr[0]);\n    }\n    if (count($geo_arr) == 1) {\n        $data[&#039;country&#039;] = trim($geo_arr[0]);\n    }\n    if (count($geo_arr) == 2) {\n        $data[&#039;country&#039;] = trim($geo_arr[1]);\n        $data[&#039;region&#039;] = trim($geo_arr[0]);\n    }\n} else if ($arr[0][0][3] &amp;&amp; strpos($arr[0][0][3], &#039;GeoIP&#039;) !== FALSE) {  \/\/\u8fc7\u6ee4\u5e7f\u544a\n    $geoip = str_replace(&quot;GeoIP:&quot;, &quot;&quot;, $arr[0][0][3]);\n    $geo_arr = explode(&quot;,&quot;,$geoip);\n    if (count($geo_arr) == 3) {\n        $data[&#039;country&#039;] = trim($geo_arr[2]);\n        $data[&#039;region&#039;] = trim($geo_arr[1]);\n        $data[&#039;city&#039;] = trim($geo_arr[0]);\n    }\n    if (count($geo_arr) == 1) {\n        $data[&#039;country&#039;] = trim($geo_arr[0]);\n    }\n    if (count($geo_arr) == 2) {\n        $data[&#039;country&#039;] = trim($geo_arr[1]);\n        $data[&#039;region&#039;] = trim($geo_arr[0]);\n    }\n}\n\nvar_dump($data);\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5ip.cn\u4e3a\u4f8b\uff0c\u6839\u636eIP\u5730\u5740\u5728\u7ebf\u67e5\u8be2\u7701\u5e02\u533a &lt;?php $url=&#039;https:\/\/ip.c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-php-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}