我想在 Laravel 5.8 中使用 Http 門面,但我注意到這個版本的 Laravel 中不包含 Http 門面,所以我安裝了 GuzzleHttp。
但現在我不知道如何用這個包重寫這段代碼:
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'));
}
那么如何使用 GuzzleHttp 正確重寫此代碼以使用Http?
uj5u.com熱心網友回復:
由于 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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/493479.html
標籤:php 拉拉维尔 http 狂饮 laravel-5.8
