{"id":22913,"date":"2024-11-21T16:05:45","date_gmt":"2024-11-21T08:05:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22913\/"},"modified":"2024-11-21T16:05:45","modified_gmt":"2024-11-21T08:05:45","slug":"thinkphp%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e5%8f%91%e5%b8%83%e8%80%85%e5%9c%b0%e5%9d%80%e6%a0%87%e6%b3%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22913\/","title":{"rendered":"ThinkPHP\u600e\u4e48\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u4f7f\u7528 IP \u5730\u5740\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8<\/strong><\/p>\n<p>\u5728\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176 IP \u5730\u5740\u8fdb\u884c\u5b9a\u4f4d\u3002\u5728 ThinkPHP \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 getRequest() \u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u8bf7\u6c42\u7684 IP \u5730\u5740\uff0c\u518d\u5229\u7528\u7b2c\u4e09\u65b9\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff08\u5982\u767e\u5ea6\u5730\u56fe API\uff09\u8fdb\u884c\u89e3\u6790\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/&nbsp;\u83b7\u53d6\u5f53\u524d\u8bf7\u6c42\u7684&nbsp;IP&nbsp;\u5730\u5740\n$client_ip&nbsp;=&nbsp;request()-&amp;gt;ip();\n\/\/&nbsp;\u5229\u7528\u767e\u5ea6\u5730\u56fe&nbsp;API&nbsp;\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\n$url&nbsp;=&nbsp;\"http:\/\/api.map.baidu.com\/location\/ip?ak=\u60a8\u7684\u5bc6\u94a5&amp;amp;ip=$client_ip&amp;amp;coor=bd09ll\";\n$result&nbsp;=&nbsp;file_get_contents($url);\n$location&nbsp;=&nbsp;json_decode($result,&nbsp;true);\n$address&nbsp;=&nbsp;$location['content']['address'];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$client_ip \u53d8\u91cf\u5b58\u50a8\u5f53\u524d\u8bf7\u6c42\u7684 IP \u5730\u5740\uff0c$url \u53d8\u91cf\u5b58\u50a8\u8c03\u7528\u767e\u5ea6\u5730\u56fe API \u7684 URL \u5730\u5740\uff0c$result \u53d8\u91cf\u5b58\u50a8\u89e3\u6790\u7ed3\u679c\uff0c$location \u53d8\u91cf\u4e3a\u89e3\u6790\u7ed3\u679c\u7684\u6570\u7ec4\u5f62\u5f0f\uff0c$address \u53d8\u91cf\u5b58\u50a8\u6700\u7ec8\u83b7\u53d6\u7684\u5730\u5740\u6807\u6ce8\u4fe1\u606f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u4e8c\u3001\u5229\u7528\u6d4f\u89c8\u5668\u5b9a\u4f4d\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 IP \u5730\u5740\u5b9a\u4f4d\u83b7\u53d6\u4f4d\u7f6e\u6807\u8bb0\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u6d4f\u89c8\u5668\u5b9a\u4f4d\u6765\u83b7\u53d6\u4f4d\u7f6e\u6807\u8bb0\u3002\u5728 ThinkPHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u83b7\u53d6\u6d4f\u89c8\u5668\u5b9a\u4f4d\uff0c\u518d\u5229\u7528\u7b2c\u4e09\u65b9\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff08\u5982\u817e\u8baf\u5730\u56fe API\uff09\u8fdb\u884c\u89e3\u6790\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684\u5730\u5740\u6807\u6ce8\u4fe1\u606f\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/&nbsp;\u9875\u9762\u4e2d\u5f15\u5165&nbsp;JavaScript&nbsp;\u5b9a\u4f4d\u4ee3\u7801\nnavigator.geolocation.getCurrentPosition(function(position)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u6d4f\u89c8\u5668\u5b9a\u4f4d\u7684\u7ecf\u7eac\u5ea6\n&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lat&nbsp;=&nbsp;position.coords.latitude;\n&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lng&nbsp;=&nbsp;position.coords.longitude;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5229\u7528\u817e\u8baf\u5730\u56fe&nbsp;API&nbsp;\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;url&nbsp;=&nbsp;\"https:\/\/apis.map.qq.com\/ws\/geocoder\/v1\/?location=\"&nbsp;+&nbsp;lat&nbsp;+&nbsp;\",\"&nbsp;+&nbsp;lng&nbsp;+&nbsp;\"&amp;amp;key=\u60a8\u7684\u5bc6\u94a5&amp;amp;get_poi=0\";\n&nbsp;&nbsp;&nbsp;&nbsp;$.ajax({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;url,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataType:&nbsp;\"jsonp\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(result)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u89e3\u6790\u7ed3\u679c\uff0c\u83b7\u53d6\u5730\u5740\u6807\u6ce8\u4fe1\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;address&nbsp;=&nbsp;result.result.formatted_addresses.recommend;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;})\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cnavigator.geolocation.getCurrentPosition() \u65b9\u6cd5\u83b7\u53d6\u6d4f\u89c8\u5668\u5b9a\u4f4d\uff0clat \u548c lng \u53d8\u91cf\u5b58\u50a8\u83b7\u53d6\u7684\u7ecf\u7eac\u5ea6\u4fe1\u606f\uff0curl \u53d8\u91cf\u5b58\u50a8\u8c03\u7528\u817e\u8baf\u5730\u56fe API \u7684 URL \u5730\u5740\uff0csuccess \u53c2\u6570\u5185\u7684\u4ee3\u7801\u83b7\u53d6\u89e3\u6790\u7ed3\u679c\uff0c\u5e76\u83b7\u53d6\u6700\u7ec8\u7684\u5730\u5740\u6807\u6ce8\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP\u600e\u4e48\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4f7f\u7528 IP \u5730\u5740\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8 \u5728\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176 IP \u5730\u5740\u8fdb\u884c\u5b9a\u4f4d\u3002\u5728 ThinkPHP \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 getRequest() \u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u8bf7\u6c42\u7684 IP \u5730\u5740\uff0c\u518d\u5229\u7528\u7b2c\u4e09\u65b9\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff08\u5982\u767e\u5ea6\u5730\u56fe API\uff09\u8fdb\u884c\u89e3\u6790\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \/\/&nbsp;\u83b7\u53d6\u5f53\u524d\u8bf7\u6c42\u7684&nbsp;IP&nbsp;\u5730\u5740 $client_ip&nbsp;=&nbsp;request()-&amp;gt;ip(); \/\/&nbsp;\u5229\u7528\u767e\u5ea6\u5730\u56fe&nbsp;API&nbsp;\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f $url&nbsp;=&nbsp;&#8220;http:\/\/api.map.baidu.com\/location\/ip?ak=\u60a8\u7684\u5bc6\u94a5&amp;amp;ip=$client_ip&amp;amp;coor=bd09ll&#8221;; $result&nbsp;=&nbsp;file_get_contents($url); $location&nbsp;=&nbsp;json_decode($result,&nbsp;true); $address&nbsp;=&nbsp;$location[&#8216;content&#8217;][&#8216;address&#8217;]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$client_ip \u53d8\u91cf\u5b58\u50a8\u5f53\u524d\u8bf7\u6c42\u7684 IP \u5730\u5740\uff0c$url \u53d8\u91cf\u5b58\u50a8\u8c03\u7528\u767e\u5ea6\u5730\u56fe API \u7684 URL \u5730\u5740\uff0c$result \u53d8\u91cf\u5b58\u50a8\u89e3\u6790\u7ed3\u679c\uff0c$location \u53d8\u91cf\u4e3a\u89e3\u6790\u7ed3\u679c\u7684\u6570\u7ec4\u5f62\u5f0f\uff0c$address \u53d8\u91cf\u5b58\u50a8\u6700\u7ec8\u83b7\u53d6\u7684\u5730\u5740\u6807\u6ce8\u4fe1\u606f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e8c\u3001\u5229\u7528\u6d4f\u89c8\u5668\u5b9a\u4f4d\u83b7\u53d6\u53d1\u5e03\u8005\u5730\u5740\u6807\u6ce8 \u9664\u4e86\u4f7f\u7528 IP \u5730\u5740\u5b9a\u4f4d\u83b7\u53d6\u4f4d\u7f6e\u6807\u8bb0\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u6d4f\u89c8\u5668\u5b9a\u4f4d\u6765\u83b7\u53d6\u4f4d\u7f6e\u6807\u8bb0\u3002\u5728 ThinkPHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u83b7\u53d6\u6d4f\u89c8\u5668\u5b9a\u4f4d\uff0c\u518d\u5229\u7528\u7b2c\u4e09\u65b9\u5730\u7406\u4f4d\u7f6e\u670d\u52a1\uff08\u5982\u817e\u8baf\u5730\u56fe API\uff09\u8fdb\u884c\u89e3\u6790\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684\u5730\u5740\u6807\u6ce8\u4fe1\u606f\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \/\/&nbsp;\u9875\u9762\u4e2d\u5f15\u5165&nbsp;JavaScript&nbsp;\u5b9a\u4f4d\u4ee3\u7801 navigator.geolocation.getCurrentPosition(function(position)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u6d4f\u89c8\u5668\u5b9a\u4f4d\u7684\u7ecf\u7eac\u5ea6 &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lat&nbsp;=&nbsp;position.coords.latitude; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lng&nbsp;=&nbsp;position.coords.longitude; &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5229\u7528\u817e\u8baf\u5730\u56fe&nbsp;API&nbsp;\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;url&nbsp;=&nbsp;&#8220;https:\/\/apis.map.qq.com\/ws\/geocoder\/v1\/?location=&#8221;&nbsp;+&nbsp;lat&nbsp;+&nbsp;&#8220;,&#8221;&nbsp;+&nbsp;lng&nbsp;+&nbsp;&#8220;&amp;amp;key=\u60a8\u7684\u5bc6\u94a5&amp;amp;get_poi=0&#8221;; &nbsp;&nbsp;&nbsp;&nbsp;$.ajax({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataType:&nbsp;&#8220;jsonp&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22913","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22913"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22913\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}