Php 将LAVEL查询字符串转换为路由中的api调用

Php 将LAVEL查询字符串转换为路由中的api调用,php,laravel,Php,Laravel,我有这条路线 Route::get('/getLocation/{postalCode}', function () { $response = Http::get('https://theapi'); return $response->json(); }); 遗憾的是,这不起作用 我可以使用 https://theapi?PostalCode=xxxx 如何在我的工作路线中复制此内容?您得到了double}}在那里,请尝试删除它,使其如下所示: Route::get

我有这条路线

Route::get('/getLocation/{postalCode}', function () {
    $response = Http::get('https://theapi');
    return $response->json();
});
遗憾的是,这不起作用

我可以使用

https://theapi?PostalCode=xxxx

如何在我的工作路线中复制此内容?

您得到了double}}在那里,请尝试删除它,使其如下所示:

Route::get('/getLocation/{postalCode}', function () {
    $response = Http::get('https://theapi');
    return $response->json();
});
编辑:

将路由参数添加到API函数参数

Route::get('/getLocation/{postalCode}', function ($postalCode) {
    // do whatever you want to postalCode variable
    $response = Http::get('https://theapi', [
        'PostalCode' => $postalCode
    ]);
    return $response->json();
});

如果有double}},请尝试将其删除,使其如下所示:

Route::get('/getLocation/{postalCode}', function () {
    $response = Http::get('https://theapi');
    return $response->json();
});
编辑:

将路由参数添加到API函数参数

Route::get('/getLocation/{postalCode}', function ($postalCode) {
    // do whatever you want to postalCode variable
    $response = Http::get('https://theapi', [
        'PostalCode' => $postalCode
    ]);
    return $response->json();
});

不工作,我需要将邮政编码作为参数发送到api。我想我需要在路由中传递值,在这个函数中访问它,然后在api上发送callI现在可以访问变量了,如果您编辑它,将这个变量发送到参数中的api,您的示例将是正确的。测试并编辑您的问题,以便其他人可以看到工作示例提醒,如果您以后需要多个变量,函数的参数将遵循路由参数顺序,命名并不重要,除非您正在使用BindingNot work,否则我需要将邮政编码作为参数发送到api。我想我需要在路由中传递值,在这个函数中访问它,然后在api上发送callI现在可以访问变量了,如果您编辑它,将这个变量发送到参数中的api,您的示例将是正确的。测试并编辑您的问题,以便其他人可以看到工作示例提醒,如果您以后需要多个变量,函数的参数将遵循路由参数顺序,除非您使用绑定,否则命名并不重要