简体   繁体   中英

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. 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 . It is not the public IP address of the server.

So how do I find out which IP address my production server is using for calling the API?

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 .

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.

The IP address of the server can be retrieved from $_SERVER['SERVER_ADDR']

You can try emailing the IP address of the server to yourself when you see the error message.

More info:

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/

PHP code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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