请求Google Maps地理编码API时偶发415 Unsupported Media Type错误求助
Google Maps地理编码API 间歇性415 Unsupported Media Type错误处理
调用Google Maps地理编码API时遇到间歇性的415 Unsupported Media Type错误,请求有时正常执行,有时失败;偶尔重试可解决,但部分场景多次重试仍报错。当前代码如下:
protected static function geocode(array $attrs): array { $options = PostTypes::get_options(); $details = array(); if (!empty($options['map_api_key'])) { $args = array( 'key' => $options['map_api_key'], 'language' => 'en', ); if (!empty($attrs['lat']) && !empty($attrs['lng'])) { $args['latlng'] = $attrs['lat'] . ',' . $attrs['lng']; } else if (!empty($attrs['address'])) { $args['address'] = $attrs['address']; } if (!empty($args['latlng']) || !empty($args['address'])) { $args = Functions::create_query($args); $response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json$args"); $status = wp_remote_retrieve_response_code($response); if (!is_wp_error($response) && 200 == $status) { $body = json_decode(wp_remote_retrieve_body($response)); if ($body->results && 0 < count($body->results)) { $details = (array)$body->results[0]; } } } } return $details; }
问题分析与修复方案
1. URL参数拼接错误
当前代码中URL拼接为json$args,如果Functions::create_query返回的参数没有以?开头,会导致URL格式错误(比如变成jsonkey=xxx),触发API解析异常。同时自定义的参数编码函数可能存在特殊字符处理缺陷,建议替换为WordPress原生的http_build_query,并确保参数前带?:
$args = http_build_query($args); $response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?$args");
2. 请求头缺失显式声明
wp_remote_get默认请求头可能偶尔不符合API预期,显式设置Accept头确保请求格式被正确识别:
$response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?$args", array( 'headers' => array( 'Accept' => 'application/json' ) ));
3. 添加指数退避重试机制
针对间歇性错误,实现带指数退避的重试逻辑,避免频繁重试加剧问题:
$max_retries = 3; $retry_delay = 1; // 初始延迟1秒 $response = null; $status = 0; for ($i = 0; $i < $max_retries; $i++) { $response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?$args", array( 'headers' => array( 'Accept' => 'application/json' ) )); $status = wp_remote_retrieve_response_code($response); if (!is_wp_error($response) && ($status == 200 || $status != 415)) { break; } sleep($retry_delay); $retry_delay *= 2; // 指数退避,延迟翻倍 }
修复后的完整代码
protected static function geocode(array $attrs): array { $options = PostTypes::get_options(); $details = array(); if (!empty($options['map_api_key'])) { $args = array( 'key' => $options['map_api_key'], 'language' => 'en', ); if (!empty($attrs['lat']) && !empty($attrs['lng'])) { $args['latlng'] = $attrs['lat'] . ',' . $attrs['lng']; } elseif (!empty($attrs['address'])) { $args['address'] = $attrs['address']; } if (!empty($args['latlng']) || !empty($args['address'])) { $args = http_build_query($args); $max_retries = 3; $retry_delay = 1; $response = null; $status = 0; // 带指数退避的重试逻辑 for ($i = 0; $i < $max_retries; $i++) { $response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?$args", array( 'headers' => array( 'Accept' => 'application/json' ) )); $status = wp_remote_retrieve_response_code($response); if (!is_wp_error($response) && ($status == 200 || $status != 415)) { break; } sleep($retry_delay); $retry_delay *= 2; } if (!is_wp_error($response) && 200 == $status) { $body = json_decode(wp_remote_retrieve_body($response)); if ($body->results && count($body->results) > 0) { $details = (array)$body->results[0]; } } } } return $details; }
内容的提问来源于stack exchange,提问作者Irwan
相关产品推荐
相关产品推荐

