[英]How to use Rewrite this code with GuzzleHttp
I wanted to use Http facade in Laravel 5.8 but I noticed that Http facade is not included in this version of Laravel so I installed GuzzleHttp.我想在 Laravel 5.8 中使用 Http 门面,但我注意到这个版本的 Laravel 中不包含 Http 门面,所以我安装了 GuzzleHttp。
But now I don't know how to rewrite this code with this package:但现在我不知道如何用这个包重写这段代码:
public function getAddress(Request $request)
{
$response=Http::timeout(15)->withHeaders([
'Api-Key' => 'api-key',
])->get('https://api.sitename.org/v4/reverse',[
"lat"=>$request->input('latitude'),
"lng"=>$request->input('longitude')
]);
$address=$response->json()['formatted_address'];
return view('address.index',compact('address'));
}
So how can I properly rewrite this code using GuzzleHttp in order to use the Http
?那么如何使用 GuzzleHttp 正确重写此代码以使用Http
?
Since guzzle follows psr-7 (I think) there is no builtin method to decode response other things or obvious to you I guess由于 guzzle 遵循 psr-7 (我认为),因此没有内置方法来解码响应其他事情或对你来说很明显我猜
try {
$client = new \GuzzleHttp\Client();
$response = $client->get('https://api.sitename.org/v4/reverse', [
RequestOptions::HEADERS => [
'Api-Key' => 'api-key',
],
RequestOptions::QUERY => [
"lat" => $request->input('latitude'),
"lng" => $request->input('longitude')
],
]);
$response = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
dd($response);
} catch (ClientException $e) {
// Handle error here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.