PHP调用Google地图地址解析时遇REQUEST_DENIED错误的解决方法
REQUEST_DENIEDNo map found Error in Google Maps Geocoding with PHP Hey, let's tackle that frustrating REQUEST_DENIEDNo map found error you're hitting. This issue almost always stems from authorization problems with Google's Geocoding API, so let's walk through the most common fixes step by step:
1. Ensure the Geocoding API is Enabled
First, jump into your Google Cloud Console:
- Locate the Geocoding API in the API library
- Check if it’s marked as "Enabled" — if not, click the "Enable" button to activate it. Google blocks all requests for APIs that aren’t turned on for your project.
2. Add a Valid API Key to Your Request
Chances are, your geocode() function isn’t including an API key in the request URL. Google now requires this for every Geocoding API call.
Update your geocode function to include the key like this:
function geocode($address){ $address = urlencode($address); // Replace with your actual API key from Google Cloud $apiKey = "YOUR_VALID_API_KEY"; $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key={$apiKey}"; $resp_json = file_get_contents($url); $resp = json_decode($resp_json, true); if($resp['status']=='OK'){ $data_arr = [ 'latitude' => $resp['results'][0]['geometry']['location']['lat'], 'longitude' => $resp['results'][0]['geometry']['location']['lng'], 'formatted_address' => $resp['results'][0]['formatted_address'] ]; return $data_arr; } else { // Print Google's specific error message for debugging echo "API Error: " . $resp['status'] . " - " . ($resp['error_message'] ?? 'No additional details'); return false; } }
Make sure to swap YOUR_VALID_API_KEY with the key you generated in your Google Cloud project.
3. Validate Your API Key's Restrictions
Even with a valid key, incorrect restrictions can block your request:
- IP Restrictions: If you set IP limits, confirm your server’s public IP is added to the allowed list.
- HTTP Referrer Restrictions: For web apps, double-check that your domain (e.g.,
https://yourdomain.com/*) is listed correctly. - Quota Limits: Verify you haven’t exceeded the Geocoding API’s free quota — you can check this in the Google Cloud Console’s quota section.
4. Debug the Request Directly
To get precise error details, copy the full $url value from your code and paste it into a browser. Google will return a JSON response with a clear message (like "API key not valid" or "API not enabled") that points you exactly to the root issue.
Following these steps should resolve the REQUEST_DENIED error and get your geocoding functionality working as expected.
内容的提问来源于stack exchange,提问作者Eric Anthony Wu

