对 api 令牌使用 Config 给出错误 'URI must be a string or UriInterface' Laravel

Using Config for api token gives error 'URI must be a string or UriInterface' Laravel

我正在尝试拨打 API 电话。

如果我这样做:$url = "url-code.com?param1=value1&param2=value2&_token=enter-key-here"; 我没有收到任何错误。

如果我这样做:$url = "url-code.com?param1=value1&param2=value2&_token="+Config::get('app.john_doe_key');

我得到一个错误:'URI must be a string or UriInterface'

我的代码

$statusCode = 200;
        $url = "url-code.com?param1=value1&param2=value2&_token="+Config::get('app.john_doe_key'); 

            $client = new Client();
            $res = $client->get($url);
            //dd($res);
            return $res->getBody();

.env

JOHN_DOE_APP_KEY=key

config/app.php

'john_doe_key' => env('JOHN_DOE_APP_KEY'),

好的,根据我们在原始问题评论中的讨论,这就是我要尝试的方法。

由于它自己的一切都正常工作,我会将所有参数放在它们自己的数组中:

$parameters = [
    'someParam' => 'value',
    'someOtherParam' => 'value',
    '_token' => Config::get('app.john_doe_key')
];

并使用 http_build_query() 正确格式化它们:

$formattedParameters = http_build_query($parameters);

最后,用我所拥有的构建URL:

$url = "http://url-code.com?{$formattedParameters}";

此时您应该具有正确格式的 URL 以与 Guzzle 一起使用。