【问题标题】:How can i get api data with params [Laravel]我如何使用参数获取 api 数据 [Laravel]
【发布时间】:2022-01-15 12:52:15
【问题描述】:

这是我的控制器代码:

public function tournaments() {
        $data = 0;
        $response = Http::withHeaders(
            [
                'x-rapidapi-host'=> 'uxxxx',
                'x-rapidapi-key'=> 'asdfasdfas',
            ]
    )->get('https://unibet.p.rapidapi.com/competitions-by-sport/football');
return $response;
}

并收到响应消息:{"message":"Missing required parameters"}

如何向这些代码添加参数?例如:params: {sport: 'football'},

也许有人可以分享一个关于如何使用 API LARAVEL 的教程视频?

【问题讨论】:

  • 你不应该在堆栈溢出时发布你的 api 密钥,你应该屏蔽它
  • 你现在应该得到一个新密钥,这篇文章的编辑历史是公开可见的

标签: php laravel api


【解决方案1】:

如果您在谈论查询参数,那就是您要查找的内容:https://laravel.com/docs/8.x/http-client#get-request-query-parameters

发出 GET 请求时,您可以将查询字符串附加到 URL 直接或传递一个键/值对数组作为第二个 get 方法的参数

下面是一个示例:

public function tournaments() {
    $data = 0;
    $response = Http::withHeaders(
        [
            'x-rapidapi-host'=> 'uxxxx',
            'x-rapidapi-key'=> 'asdfasdfas',
        ]
    )->get('https://unibet.p.rapidapi.com/competitions-by-sport', [
        'sport' => 'football'
    ]);
    return $response;
}

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 2021-06-20
    • 1970-01-01
    • 2016-06-05
    • 2017-12-06
    • 2020-05-30
    • 2020-01-21
    • 1970-01-01
    • 2019-10-19
    相关资源
    最近更新 更多