我用哪个IP地址来调用外部API?

时间:2014-06-21 19:15:20

标签: php google-maps google-maps-api-3

我有一个PHP脚本,用于使用Google Geocode API对地址进行地理编码。这在我的测试服务器上运行良好。我使用网址

https://maps.googleapis.com/maps/api/geocode/json?key=SECRET&address=ADDRESS&sensor=false

但是当部署到生产服务器时,我收到以下错误消息:

{ "error_message" : "This IP, site or mobile application is not authorized to use this API key.", "results" : [], "status" : "REQUEST_DENIED" }

服务器有多个IP地址,我不知道IP范围或所有IP地址,因此,我无法在Google's API console中添加它们。 不是服务器的公共IP地址

那么如何找出我的生产服务器用于调用API的IP地址?

3 个答案:

答案 0 :(得分:1)

我找到了问题的解决方法,然而,它并没有真正回答我原来的问题,我会将其打开。

根据某些well-hidden Google API documentation,可以添加userIp - 参数。

所以我成功使用了电话

https://maps.googleapis.com/maps/api/geocode/json?key=SECRET&address=ADDRESS&sensor=false&userIp=11.22.33.44

其中IP地址是允许的IP地址之一。

答案 1 :(得分:0)

可以从$_SERVER['SERVER_ADDR']

检索服务器的IP地址

当您看到错误消息时,可以尝试通过电子邮件将服务器的IP地址发送给自己。

更多信息:

http://www.php.net/manual/en/reserved.variables.server.php

答案 2 :(得分:0)

通过向http://ip.appspot.com/发送HTTP请求,可以找到外部请求中使用的服务器的IP地址

PHP代码:

<?php
echo file_get_contents('http://ip.appspot.com/');
?>