如何在php中使用api ipinfodb [英] How to use api ipinfodb in php
问题描述
我正在尝试在我的服务器上的PHP代码中使用免费的api ipinfodb服务。
我用服务注册了我的服务器ip,我得到了我的API密钥。
I am trying to use the free api ipinfodb service in php code on my server. I registered my server ip with the service and I got my API key.
如果我直接通过我的浏览器运行这个服务:
If I run the service directly through my browser like this:
http://api.ipinfodb.com/v3/ip-city/?key=<apikey>&ip=117.212.155.134
它运作良好并快速返回如下字符串:
It works well and quickly returns a string like this:
OK;;117.212.155.134;IN;INDIA;MAHARASHTRA;CHIPLUN;415726;17.5333;73.5167;+05:30
然而,当我尝试在我的服务器上通过php运行相同的东西时,它会永久计时并因超时错误而失败。
However, when I try to run the same thing through php on my server, it clocks forever and fails with a timeout error.
此逻辑适用于我的本地服务器,但不适用于我的远程HostGator服务器。
如果我用其他网站地址替换api.infodb.com,则会返回正确的结果。
几乎就像api.ipinfodb.com阻止我的hostgator服务器拨打电话一样。
我用api.ipinfodb.com和我的api密钥注册了我的hostgator服务器ip地址。
This logic works on my local server, but not on my remote HostGator server. If I replace api.infodb.com with another website address, a proper result is returned. It's almost as if api.ipinfodb.com is blocking my hostgator server from making the call. I did register my hostgator server ip address with api.ipinfodb.com and my api key.
什么导致超时错误?
<?php
$ip = "117.212.155.134";
$lookup = sprintf('http://api.ipinfodb.com/v3/ip-city/?key=<apikey>&ip=%s', $ip);
$result = trim(file_get_contents($lookup));
echo $result . "<br />\n";
推荐答案
使用它来获取确切的输出使用浏览器。
Use this to get the exact output as you do when using the browser.
$arr_location=file_get_contents('http://api.ipinfodb.com/v3/ip-city/?key=**YOURKEY**&ip='.**$THE_IP_TO_LOOKUP**);
print_r($arr_location);
这篇关于如何在php中使用api ipinfodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!