1
votes

I'm trying to update to Guzzle 6 and in the changelog you have this:

Static functions in GuzzleHttp\Utils have been moved to namespaced functions under the GuzzleHttp namespace. This requires either a Composer based autoloader or you to include functions.php.

Now I have tried different ways of autoloading the functions but I'm getting different errors. What is the proper way of autoloading the functions on composer.json

I have added this to my composer.json:

"autoload": {
    "files": ["vendor/guzzlehttp/guzzle/src/functions.php"]
}

Then after that I get an error:

PHP Fatal error: Cannot redeclare GuzzleHttp\uri_template() (previously declared in /home/fabio/flubit/dm/vendor/guzzlehttp/guzzle/src/functions.php:18) in /home/fabio/flubit/dm/vendor/guzzlehttp/guzzle/src/functions.php on line 32

So obviously I'm trying to load something that's already being loaded.

So I removed the autoload from composer and then try to use the json_decode()built in function on Guzzle doing this \GuzzleHttp\json_decode() I get this:

PHP Fatal error: Call to undefined function GuzzleHttp\json_decode()

1
Are you using the Composer autoloader? - ceejayoz
@ceejayoz yes look at my updated question - Fabio Antunes
I'd a) remove the "autoload" section you added to composer.json (Guzzle already gets loaded automatically, you're probably double-loading it) and if that doesn't fix it b) remove the entire vendor/ directory and do a fresh composer install. Also, Guzzle doesn't have a json_decode function - PHP does. \GuzzleHttp\json_decode() won't ever work. - ceejayoz
@ceejayoz you are right, the thing was my Phpstorm was doing a search on my repo that has the old version of Guzzle and in there the json_decode() indeed exists, then they changed it do jsonDecode() on version 5 and it seems on guzzle 6 they completely removed. So the problem wasn't on the autoload but in the fact I was looking at the wrong Guzzle version thinking that I was looking at version 6 - Fabio Antunes

1 Answers

0
votes

The Guzzle does provide the 'json' Request Option which can simplify the creation of sending json encoded requests. It will automatically encode using PHP's json_encode() function and then set the appropriate content type header.