Search code examples
phplaravellaravel-5.3

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


I am trying to make an API call.

If I do this: $url = "url-code.com?param1=value1&param2=value2&_token=enter-key-here"; I don't get any error.

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

I get an error: 'URI must be a string or UriInterface'

mycode

$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'),

Solution

  • All right, based on our discussion in the comments of original question, here's what I would try.

    Since everything in it's own works correctly, I would put all of the parameters in their own array:

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

    And use http_build_query() to correctly format them:

    $formattedParameters = http_build_query($parameters);
    

    And finally, build the URL with what I have:

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

    You should be having a correctly formatted URL to use with Guzzle at this point.