[英]Which IP address am I using to call an external API?
I have a PHP script that I use to geocode addresses using the Google Geocode API. 我有一个PHP脚本,可用于使用Google Geocode API对地址进行地理编码。 This works well on my test server. 这在我的测试服务器上运行良好。 I use the URL 我使用网址
https://maps.googleapis.com/maps/api/geocode/json?key=SECRET&address=ADDRESS&sensor=false
But when deployed to the production server, I get this error message: 但是,当部署到生产服务器时,我收到此错误消息:
{ "error_message" : "This IP, site or mobile application is not authorized to use this API key.", "results" : [], "status" : "REQUEST_DENIED" }
The server has multiple IP addresses, and I do not know the IP range or all of the IP addresses, and therefore, I am unable to add them in Google's API console . 该服务器具有多个IP地址,并且我不知道IP范围或所有IP地址,因此,我无法在Google的API控制台中添加它们。 It is not the public IP address of the server. 它不是服务器的公共IP地址 。
So how do I find out which IP address my production server is using for calling the API? 那么,如何找出生产服务器用来调用API的IP地址?
I found a workaround for the problem, however, it does not really answer my original question, which I will leave open. 我找到了解决该问题的方法,但是它并不能真正回答我原来的问题,我将继续公开。
It is possible to add the userIp
-parameter, according to some well-hidden Google API documentation . 根据一些隐藏的Google API文档 ,可以添加userIp
参数。
So I successfully used the call 所以我成功使用了通话
https://maps.googleapis.com/maps/api/geocode/json?key=SECRET&address=ADDRESS&sensor=false&userIp=11.22.33.44
where the IP address was one of the allowed IP-addresses. IP地址是允许的IP地址之一。
The IP address of the server can be retrieved from $_SERVER['SERVER_ADDR']
可以从$_SERVER['SERVER_ADDR']
检索服务器的IP地址。
You can try emailing the IP address of the server to yourself when you see the error message. 看到错误消息后,您可以尝试通过电子邮件将服务器的IP地址发送给自己。
More info: 更多信息:
http://www.php.net/manual/en/reserved.variables.server.php http://www.php.net/manual/en/reserved.variables.server.php
The IP address of the server which is being used on external request can be found by making a HTTP request to http://ip.appspot.com/ 通过向http://ip.appspot.com/发出HTTP请求,可以找到用于外部请求的服务器的IP地址。
PHP code: PHP代码:
<?php
echo file_get_contents('http://ip.appspot.com/');
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.