如何解决发出浏览器外请求时 Google 距离矩阵 URL 出现问题
API 密钥具有链接到它的距离矩阵 API。如果您使用自己的密钥,您将在浏览器中看到请求正常工作。但是,当您在代码中创建 HTTP::get() 时,它会返回 404。目前,我正在使用 PHP Laravel 并遵循文档。
这是请求代码:
$response = Http::get('https://maps.googleapis.com/maps/api/distancematrix/json
?units=metric
&origins=Neuh%C3%B6fer+damm+110,Germany
&destinations=Vogelweide+8,Hamburg
&key=MyKey`enter code here`'
);
这是给定的回复:
Illuminate\Http\Client\Response {#576 ▼
#response: GuzzleHttp\Psr7\Response {#618 ▼
-reasonPhrase: "Not Found"
-statusCode: 404
-headers: array:8 [▼
"Date" => array:1 [▶]
"Content-Type" => array:1 [▶]
"Server" => array:1 [▶]
"Content-Length" => array:1 [▶]
"X-XSS-Protection" => array:1 [▶]
"x-frame-options" => array:1 [▶]
"server-timing" => array:1 [▶]
"Alt-Svc" => array:1 [▶]
]
-headerNames: array:8 [▶]
-protocol: "1.1"
-stream: GuzzleHttp\Psr7\Stream {#617 ▶}
您是否遇到同样的问题?那么主要的问题是:当我们使用相同的有效链接时,我们会错过什么所以我们得到 NotFound?也许标题?也许别的什么?我会继续寻找更合适的答案,但同时,我希望能有所帮助
解决方法
- 请勿使用换行符,因为可能会在请求中添加空格。
- 不要在代码中加入一行包含大量参数的不可读的巨大 URL 行,而是尝试使用 GET 请求查询参数。我认为大多数 HTTP 客户端都可以做到这一点。在使用 Laravel PHP 框架的情况下,整个事情应该是这样的:
$response = Http::get('https://maps.googleapis.com/maps/api/distancematrix/json',[
'units' => 'metric','origins' => 'Neuhöfer Damm 110,Germany','destinations' => 'Vogelweide 8,Hamburg','key' => YOUR_API_KEY'
]);
享受你的编码 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。