Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'),
like image 929
Murlidhar Fichadia Avatar asked Nov 25 '25 01:11

Murlidhar Fichadia


1 Answers

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.

like image 52
Jean-Philippe Murray Avatar answered Nov 27 '25 14:11

Jean-Philippe Murray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!