Rosamunda
Rosamunda

Reputation: 14990

Error with GuzzleHttp trying to decode a JSON string into an array in a php 7 installation

I'm trying to convert a json string into an array with php.

My php version is 7.0

I'm getting this error:

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

Here's the code with the issue:

$adjunto1 = $value['archivo']; 
$adjunto2 = json_decode($adjunto1, TRUE); //this is teh line with the error

$value['archivo'] comes from the database, and if I see inside I get:

string(155) "{"nombre":["ejemploxls"],"archivoContenido":["id.--ejemploxls--fecha-26-04-2020-10-08.xls"],"fecha":["26-04-2020-10-08"],"size":[5632],"extension":["xls"]}"

When I converted the array to a json string using json_encode(), the array looked like this:

  array(5) {
    ["name"]=>
    string(11) "ejemplo.xls"
    ["type"]=>
    string(24) "application/vnd.ms-excel"
    ["tmp_name"]=>
    string(14) "/tmp/php7M0gVS"
    ["error"]=>
    int(0)
    ["size"]=>
    int(5632)
  }

What is this error? What is GuzzleHttp?

Upvotes: 3

Views: 2258

Answers (1)

Maksim Yakunin
Maksim Yakunin

Reputation: 458

Seems that you have some kind of implicit import from GuzzleHttp.

Guzzle is an extensible PHP HTTP client.

Here is a wrapper for json_decode php-function from GuzzleHttp namespace, that may cause your problem.

As a quick soluiton just add a slash before function call:

$adjunto2 = \json_decode($adjunto1, TRUE);

If you want to obtain much more detailed answer from me, plese, add a list of you use-statements from beginning of your php-file. Also list of includes (if there is any) would be helpful. I need some more context to be more precise.

Upvotes: 2

Related Questions