{"id":445,"date":"2025-06-10T20:33:16","date_gmt":"2025-06-10T12:33:16","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/445.html"},"modified":"2025-06-10T20:33:16","modified_gmt":"2025-06-10T12:33:16","slug":"php%ef%bc%9a%e9%80%9a%e8%bf%87ip%e5%9c%b0%e5%9d%80%e8%8e%b7%e5%8f%96%e5%9f%8e%e5%b8%82%e5%92%8c%e5%9b%bd%e5%ae%b6%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/445.html","title":{"rendered":"PHP\uff1a\u901a\u8fc7IP\u5730\u5740\u83b7\u53d6\u57ce\u5e02\u548c\u56fd\u5bb6\u7684\u4e09\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p><body><\/p>\n<h2>&#27010;&#36848;<\/h2>\n<p>&#21457;&#29616;&#29992;&#25143;&#30340;&#20301;&#32622;&#22522;&#20110;&#20182;&#20204;&#30340;IP&#22320;&#22336;&#22312;&#21508;&#31181;&#32593;&#32476;&#24212;&#29992;&#31243;&#24207;&#20013;&#38750;&#24120;&#26377;&#29992;&#12290;&#26080;&#35770;&#26159;&#26412;&#22320;&#21270;&#20869;&#23481;&#12289;&#23454;&#26045;&#22320;&#29702;&#38480;&#21046;&#36824;&#26159;&#31616;&#21333;&#22320;&#25910;&#38598;&#20998;&#26512;&#25968;&#25454;&#65292;PHP&#25552;&#20379;&#20102;&#22810;&#31181;&#26041;&#27861;&#26469;&#23454;&#29616;&#36825;&#19968;&#30446;&#26631;&#12290;&#26412;&#25991;&#23558;&#25506;&#35752;&#20351;&#29992;PHP&#33719;&#21462;IP&#22320;&#22336;&#23545;&#24212;&#30340;&#24066;&#25110;&#22269;&#23478;&#30340;&#26041;&#27861;&#65292;&#35752;&#35770;&#20854;&#23454;&#29616;&#26041;&#24335;&#65292;&#24182;&#27604;&#36739;&#23427;&#20204;&#30340;&#20248;&#28857;&#21644;&#32570;&#28857;&#12290;<\/p>\n<h2>&#35299;&#20915;&#26041;&#26696; 1&#65306;&#20351;&#29992; GeoIP &#25193;&#23637;<\/h2>\n<p>GeoIP&#25193;&#23637;&#20801;&#35768;&#24744;&#26597;&#25214;IP&#22320;&#22336;&#30340;&#20301;&#32622;&#12290;&#27492;&#26041;&#27861;&#38656;&#35201;&#23433;&#35013;GeoIP PHP&#25193;&#23637;&#21644;&#21487;&#33021;&#30340;GeoIP&#25968;&#25454;&#24211;&#12290;<\/p>\n<p>&#27493;&#39588;2&#65306;&#20351;&#29992;<code>geoip_record_by_name()<\/code>&#20026;&#20102;&#33719;&#21462;&#20301;&#32622;&#20449;&#24687;&#12290;<\/p>\n<p>&#27493;&#39588; 1&#65306;&#23433;&#35013; GeoIP &#25193;&#23637;&#21644; GeoIP &#25968;&#25454;&#24211;&#12290;<\/p>\n<p>&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>$ip = '8.8.8.8';\n$record = geoip_record_by_name($ip);\nif ($record) {\n    echo 'City: ' . $record['city'] . \"n\";\n    echo 'Country: ' . $record['country_name'] . \"n\";\n}\n<\/code><\/pre>\n<p>&#25552;&#31034;&#65306;&#34429;&#28982;GeoIP&#25193;&#23637;&#21151;&#33021;&#24378;&#22823;&#65292;&#20294;&#38656;&#35201;&#39069;&#22806;&#30340;&#35774;&#32622;&#21644;&#25968;&#25454;&#20934;&#30830;&#24615;&#20381;&#36182;&#20110;&#25968;&#25454;&#24211;&#30340;&#26356;&#26032;&#39057;&#29575;&#12290;&#19968;&#26086;&#35774;&#32622;&#22909;&#65292;&#23427;&#23601;&#26159;&#19968;&#20010;&#24555;&#36895;&#35299;&#20915;&#26041;&#26696;&#12290;<\/p>\n<h2>&#35299;&#20915;&#26041;&#26696;2&#65306;&#20351;&#29992;&#31532;&#19977;&#26041;API&#26381;&#21153;<\/h2>\n<p>&#20960;&#31181;&#32593;&#32476;&#26381;&#21153;&#25552;&#20379;IP&#22320;&#29702;&#20301;&#32622;&#26381;&#21153;&#12290;&#23545;&#20110;&#36825;&#31181;&#26041;&#27861;&#65292;&#20320;&#38656;&#35201;&#27880;&#20876;&#19968;&#20010;API&#24182;&#23558;&#20854;&#29992;&#20110;&#20320;&#30340;PHP&#20195;&#30721;&#12290;&#27969;&#34892;&#30340;&#25552;&#20379;&#21830;&#21253;&#25324;IPInfo&#12289;IPStack&#21644;MaxMind&#30340;GeoIP2&#12290;<\/p>\n<p>&#27493;&#39588;3&#65306;&#35299;&#30721;JSON&#21709;&#24212;&#20197;&#33719;&#21462;&#22478;&#24066;\/&#22269;&#23478;&#20449;&#24687;&#12290;<\/p>\n<p>&#27493;&#39588;2&#65306;&#20351;&#29992;PHP&#30340;<code>file_get_contents()<\/code>&#21734;&#65292;&#26159;&#30340;&#12290;<code>curl<\/code>&#25191;&#34892;&#19968;&#20010;API&#35831;&#27714;&#12290;<\/p>\n<p>&#27493;&#39588;1&#65306;&#27880;&#20876;API&#26381;&#21153;&#24182;&#33719;&#21462;API&#23494;&#38053;&#12290;<\/p>\n<p>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>$apiKey = 'YOUR_API_KEY';\n$ip = '8.8.8.8';\n$url = \"http:\/\/api.ipstack.com\/$ip?access_key=$apiKey\";\n$response = file_get_contents($url);\n$location = json_decode($response, true);\nif ($location) {\n    echo 'City: ' . $location['city'] . \"n\";\n    echo 'Country: ' . $location['country_name'] . \"n\";\n}\n<\/code><\/pre>\n<p>&#25552;&#31034;&#65306;&#20351;&#29992;&#31532;&#19977;&#26041;API&#24456;&#23481;&#26131;&#23454;&#29616;&#65292;&#19981;&#38656;&#35201;&#32500;&#25252;&#25968;&#25454;&#24211;&#12290;&#28982;&#32780;&#65292;&#23427;&#21487;&#33021;&#20276;&#38543;&#30528;&#25104;&#26412;&#12289;&#36895;&#29575;&#38480;&#21046;&#21644;&#20381;&#36182;&#22806;&#37096;&#26381;&#21153;&#21487;&#29992;&#24615;&#30340;&#38382;&#39064;&#12290;<\/p>\n<h2>&#35299;&#20915;&#26041;&#26696; 3&#65306;PHP &#20869;&#32622;&#30340; GeoIP &#20989;&#25968;&#65288;&#33258; PHP 8 &#29256;&#26412;&#36215;&#65289;<\/h2>\n<p>PHP 8 &#24341;&#20837;&#20102;&#23545; GeoIP &#21151;&#33021;&#30340;&#21407;&#29983;&#25903;&#25345;&#65292;&#20351;&#24471;&#33021;&#22815;&#20197;&#26356;&#21152;&#31616;&#27905;&#30340;&#26041;&#24335;&#23454;&#29616;&#65292;&#26080;&#38656;&#20381;&#36182;&#22806;&#37096;&#25193;&#23637;&#12290;<\/p>\n<p>&#27493;&#39588;2&#65306;&#20351;&#29992;<code>geoip_country_name_by_name()<\/code>&#22269;&#23478;&#20449;&#24687;&#21151;&#33021;&#25110;&#26597;&#25214;&#22478;&#24066;&#25968;&#25454;&#30456;&#20284;&#30340;&#21151;&#33021;&#12290;<\/p>\n<p>&#27493;&#39588; 1&#65306;&#30830;&#20445;&#24744;&#30340; PHP &#23433;&#35013;&#29256;&#26412;&#20026; 8 &#25110;&#26356;&#39640;&#65292;&#19988;&#24050;&#21551;&#29992; GeoIP &#25903;&#25345;&#12290;<\/p>\n<p>&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>$ip = '8.8.8.8';\n$country = geoip_country_name_by_name($ip);\nif ($country) {\n    echo 'Country: ' . $country . \"n\";\n}\n<\/code><\/pre>\n<p>&#25552;&#31034;&#65306;&#36825;&#31181;&#26041;&#27861;&#25552;&#20379;&#20102;&#19968;&#20010;&#26412;&#22320;&#21270;&#30340;&#12289;&#21487;&#33021;&#26356;&#39640;&#25928;&#30340;&#21151;&#33021;&#35299;&#20915;&#26041;&#26696;&#65292;&#20294;&#23454;&#38469;&#21151;&#33021;&#21644;&#25968;&#25454;&#21487;&#29992;&#24615;&#21462;&#20915;&#20110;PHP&#30340;&#23454;&#29616;&#20197;&#21450;&#31995;&#32479;&#20013;&#30340;GeoIP&#24211;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#32508;&#19978;&#25152;&#36848;&#65292;&#36890;&#36807;PHP&#20174;&#29992;&#25143;IP&#22320;&#22336;&#33719;&#21462;&#22320;&#29702;&#20301;&#32622;&#21487;&#20197;&#36890;&#36807;&#22810;&#31181;&#26041;&#24335;&#23454;&#29616;&#65292;&#27599;&#31181;&#26041;&#27861;&#37117;&#26377;&#20854;&#20248;&#28857;&#21644;&#32570;&#28857;&#12290;&#24744;&#21487;&#20197;&#36873;&#25321;&#20351;&#29992;GeoIP&#25193;&#23637;&#12289;&#31532;&#19977;&#26041;API&#25110;&#21033;&#29992;PHP&#20869;&#32622;&#20989;&#25968;&#20013;&#30340;&#26041;&#27861;&#12290;&#22312;&#32771;&#34385;&#23454;&#26045;&#22797;&#26434;&#24615;&#12289;&#25968;&#25454;&#20934;&#30830;&#24615;&#21644;&#28508;&#22312;&#25104;&#26412;&#26102;&#65292;&#35831;&#20180;&#32454;&#35780;&#20272;&#21487;&#29992;&#36873;&#39033;&#65292;&#20197;&#20415;&#20026;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#36873;&#25321;&#26368;&#36866;&#21512;&#30340;&#35299;&#20915;&#26041;&#26696;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#27010;&#36848; &#21457;&#29616;&#29992;&#25143;&#30340;&#20301;&#32622;&#22522;&#20110;&#20182;&#20204;&#30340;IP&#22320;&#22336;&#22312;&#21508;&#31181;&#038;#325..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-445","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=445"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=445"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}