Laravel - 如何使用 guzzle 将变量作为参数传递给外部 API
Laravel - How to pass variable as parameter to external API using guzzle
我正在使用 Laravel-5.8 和 Guzzle 来消耗外部 api:
public function index()
{
try{
$myparam = 'JKK123';
$client = new Client();
$res = $client->request('GET','https://examplet/tracking/$myparam', [
'query' => ['key' => 'jkkffd091']
])->getBody();
$geoLocation = json_decode($res->getContents(), true);
$currentLocation = collect($geoLocation)->sortByDesc(function ($data) {
return Carbon::parse($data['Timestamp'])->format('d-m-Y h:i:s');
})->first();
$currentLocationFilter = explode(',', $currentLocation['current_asset_position_coord'] ?? '');
dd($currentLocationFilter);
return view('index', [
'currentLocation' => $currentLocation,
'currentLocationFilter' => $currentLocationFilter,
'geoLocation' => $geoLocation
]);
}catch (Exception $exception) {
Log::error($exception);
return back();
}
}
我正在尝试将变量作为参数传递给 API。我没有直接放,因为它会改变。我只是想测试一下。
当我按照上面的代码执行此操作时
$res = $client->request('GET','https://examplet/tracking/$myparam', [ ...
然后
dd($currentLocationFilter);
我得到了:
array:1 [▼
0 => ""
]
但是当我直接放值的时候,
$res = $client->request('GET','https://examplet/tracking/JKK123', [
我得到了要求的结果:
array:2 [▼
0 => "2.1234565432145"
1 => "1.7864321249555"
]
如何将变量作为参数传递到外部 API?
谢谢
像这样使用double quotes for executing variable:
$res = $client->request('GET',"https://examplet/tracking/{$myparam}", [...
我正在使用 Laravel-5.8 和 Guzzle 来消耗外部 api:
public function index()
{
try{
$myparam = 'JKK123';
$client = new Client();
$res = $client->request('GET','https://examplet/tracking/$myparam', [
'query' => ['key' => 'jkkffd091']
])->getBody();
$geoLocation = json_decode($res->getContents(), true);
$currentLocation = collect($geoLocation)->sortByDesc(function ($data) {
return Carbon::parse($data['Timestamp'])->format('d-m-Y h:i:s');
})->first();
$currentLocationFilter = explode(',', $currentLocation['current_asset_position_coord'] ?? '');
dd($currentLocationFilter);
return view('index', [
'currentLocation' => $currentLocation,
'currentLocationFilter' => $currentLocationFilter,
'geoLocation' => $geoLocation
]);
}catch (Exception $exception) {
Log::error($exception);
return back();
}
}
我正在尝试将变量作为参数传递给 API。我没有直接放,因为它会改变。我只是想测试一下。
当我按照上面的代码执行此操作时
$res = $client->request('GET','https://examplet/tracking/$myparam', [ ...
然后
dd($currentLocationFilter);
我得到了:
array:1 [▼
0 => ""
]
但是当我直接放值的时候,
$res = $client->request('GET','https://examplet/tracking/JKK123', [
我得到了要求的结果:
array:2 [▼
0 => "2.1234565432145"
1 => "1.7864321249555"
]
如何将变量作为参数传递到外部 API?
谢谢
像这样使用double quotes for executing variable:
$res = $client->request('GET',"https://examplet/tracking/{$myparam}", [...