【发布时间】:2021-06-24 23:16:30
【问题描述】:
我正在使用 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?
谢谢
【问题讨论】: