简体   繁体   English

如何在PHP中使用api ipinfodb

[英]How to use api ipinfodb in php

I am trying to use the free api ipinfodb service in php code on my server. 我正在尝试在服务器上的php代码中使用免费的api ipinfodb服务。 I registered my server ip with the service and I got my API key. 我使用该服务注册了服务器IP,并且获得了API密钥。

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

However, when I try to run the same thing through php on my server, it clocks forever and fails with a timeout error. 但是,当我尝试通过服务器上的php运行相同的内容时,它会一直计时并会因超时错误而失败。

This logic works on my local server, but not on my remote HostGator server. 此逻辑在我的本地服务器上有效,但在我的远程HostGator服务器上无效。 If I replace api.infodb.com with another website address, a proper result is returned. 如果我用另一个网站地址替换api.infodb.com,则会返回正确的结果。 It's almost as if api.ipinfodb.com is blocking my hostgator server from making the call. 几乎就像api.ipinfodb.com阻止了hostgator服务器发出呼叫一样。 I did register my hostgator server ip address with api.ipinfodb.com and my api key. 我确实使用api.ipinfodb.com和api密钥注册了hostgator服务器的IP地址。

What is causing the timeout error? 是什么导致超时错误?

<?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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM