Home > Blockchain >  function that returns the result of calling json_decode return a string
function that returns the result of calling json_decode return a string

Time:06-02

I am working on an application written in php 7.1.33 and for some unknown reason a strange behavior is occurring.

A function that returns the result of calling json_decode is returning a string. The code is similar to the following

public function post($url, array $body = [])
{
    try
    {
        $client = $this->getClient();
        $response = $client->request('POST', $url);

        $content = $response->getBody()->getContents();
        $output = json_decode($content);

        return $content;
    }
    catch (ClientException $e)
    {
        echo $e->getMessage();
    }
}

For some reason the call to the post function returns a string.

In fact, the problem is more complex for me because if you notice in this section

$content = $response->getBody()->getContents();
$output = json_decode($content);

return $content;

I must do it like this because if I try this way it returns null

return json_decode($response->getBody()->getContents());

The processed json is similar to this

'{
  "id": 5895018,
  "first_name": "Zu",
  "last_name": "Mr",
  "email": "[email protected]",
  "phone": null,
  "birthdate": null,
  "postcode": null,
  "address": null,
  "city": null,
  "country": null,
  "gender": null,
  "virtual_money": "0.0",
  "money": "0.0",
  "global_refundable_money": "0.0",
  "balances": { "60402": 0.0, "60403": 0.0, "60464": 0.0, "60463": 0.0 },
  "locale": "en",
  "anonymous": false,
  "refundable": true,
  "family_id": null,
  "family_owner": false,
  "gtags": [],
  "tickets": [],
  "orders": []
}'

I have tried in my local that has php 7.4.9 to reproduce the scenario but the result is as expected

function simple_test() {
    $json = '{"name": "ada", "last_name": "wong", "email": "[email protected]"}';

    return json_decode($json);
}

$output = simple_test();

var_dump($output);

Output is

class stdClass#1 (3) {                                                                                                                                                
  public $name =>                                                                                                                                                     
  string(3) "ada"                                                                                                            
  public $last_name =>                                                                                                                                                
  string(4) "wong"                                                                                                                                                    
  public $email =>                                                                                                                                                    
  string(21) "[email protected]"
}

What worries me is that moving this to production will cause a conflict

Thanks in advance for any ideas

CodePudding user response:

You are returning $content that contains the return value of $response->getBody()->getContents(). You should return $output instead.

$content = $response->getBody()->getContents();
$output = json_decode($content);

return $output;

CodePudding user response:

You can try convert same as :

$json= '{
  "id": 5895018,
  "first_name": "Zu",
  "last_name": "Mr",
  "email": "[email protected]",
  "phone": null,
  "birthdate": null,
  "postcode": null,
  "address": null,
  "city": null,
  "country": null,
  "gender": null,
  "virtual_money": "0.0",
  "money": "0.0",
  "global_refundable_money": "0.0",
  "balances": { "60402": 0.0, "60403": 0.0, "60464": 0.0, "60463": 0.0 },
  "locale": "en",
  "anonymous": false,
  "refundable": true,
  "family_id": null,
  "family_owner": false,
  "gtags": [],
  "tickets": [],
  "orders": []
}';
$object = (object)$json;

$result  = json_decode($object->scalar);

Link example

  •  Tags:  
  • php
  • Related