0

I am absolutly new in PHP (I came from Java that is strongly typed) and I am going crazy trying to understand why I can't convert into a string this object and some object concept in PHP.

So I have this code doLogin() method (into a Laravel controller class):

public function doLogin(){

    $userName = Input::get('username');
    $pswd = Input::get('password');

    //------------------------------------------
    // Guzzle clint used to perform REST call:
    //------------------------------------------
    $client = new Client(); //GuzzleHttp\Client

    //----------------------------------------------------------------------------------------------
    // Do the REST call and obain the response:
    //----------------------------------------------------------------------------------------------
    $response = $client->get('http://localhost:8080/Accomodation/7');

    // Decode the response into an object:
    $dettaglioHotel = json_decode($response->getBody());

    //Console::info('username: ' + $userName);
    //Console::info('password ' + $pswd);

    \Log::info('username: '.$userName);
    \Log::info('password '.$pswd);
    //\Log::info('hotel: '.$dettaglioHotel);
    \Log::info('response status code: '.(string)$response->getStatusCode());
    \Log::info('response: '.(string)$dettaglioHotel);

    return view('dashboard-hotel');
}

So as you can see in the previus method I am using Guzzle library to perform a REST call.

My doubts are:

1) Into PhpStorm (the used IDE), passing the mouse on the get() method it say to me that the returned object have type ResponseInterface that is an interface and not a concrete class type.

So what exactly means? That doing:

$response = $client->get('http://localhost/Accomodation/7');

it return a generic object that implements the ResponseInterface interface? Php is automatically building something like a generic object implementing this interface or what?

2) Passing the mouse on the json_decode() function, this line:

$dettaglioHotel = json_decode($response->getBody());

the IDE say a message as "Multiple implementation" and show that there are these 2 implementations:

FIRST IMPLEMENTATION: json_decode .../vendor/guzzlehttp/guzzle/src/functions.php json_decode

SECOND IMPLEMENTATION: C:/Program Files (x86)/JetBrains/PhpStorm 2016.3.2/plugins/php/lib/php.jar!/stubs/standard/json.php

It have to use the first one (related to Guzzle) but I am not sure that is using the right one.

3) Finnally I am trying to convert into a String and print in the log this object containing the response decodification, in this way:

$dettaglioHotel = json_decode($response->getBody());

and then:

\Log::info('response: '.(string)$dettaglioHotel);

But doing in this way when try to convert into strung (by (string)$dettaglioHotel) the application crash and I obtain this error message:

in LoginBetriviusController.php line 56
at HandleExceptions->handleError('4096', 'Object of class stdClass could not be converted to string', 'C:\Users\Andrea\Documents\Betrivius\WorkSpace\betriviusExtranet\app\Http\Controllers\LoginBetriviusController.php', '56', array('userName' => 'aaa', 'pswd' => 'ddd', 'client' => object(Client), 'response' => object(Response), 'dettaglioHotel' => object(stdClass))) in LoginBetriviusController.php line 56
at LoginBetriviusController->doLogin()
at call_user_func_array(array(object(LoginBetriviusController), 'doLogin'), array()) in Controller.php line 55
at Controller->callAction('doLogin', array()) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(LoginBetriviusController), 'doLogin') in Route.php line 190
at Route->runController() in Route.php line 144
at Route->run(object(Request)) in Router.php line 653
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 655
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\Users\Andrea\Documents\Betrivius\WorkSpace\betriviusExtranet\public\index.php') in server.php line 21

What it exactly means? Why I can't convert it into a string? How can I see the content of this object?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • I think the last bit makes sense since `json_decode` would return an `stdObject` which doesn't implement `__toString` https://3v4l.org/cX18r. I suppose you could log `$response->getBody()` which is already a string? – 321zeno Jan 24 '17 at 19:47

2 Answers2

0
  1. … passing the mouse on the get() method it say to me that the returned object have type ResponseInterface that is an interface and not a concrete class type

Well, the method will return some concrete object of a specific type. PHP won't do any magic with some ad-hoc object as you suggest. It simply means that the object returned by get() will adhere to (implement) the ResponseInterface. You can treat the returned object as possessing all methods and properties described in that interface. What class instance exactly will be used is irrelevant. You use interfaces instead of concrete classes wherever possible to keep flexibility in your implementation to swap out concrete classes when necessary, yet still being compatible with a defined interface.

  1. the IDE say a message as "Multiple implementation" and show that there are these 2 implementations

That means the same function is implemented twice in different files. Which version will be used will be decided at runtime based on various factors. Most likely Guzzle carries a fallback implementation of json_decode in case the PHP installation doesn't have it installed. At runtime Guzzle would conditionally define json_decode as a polyfill if necessary. This does not typically have to concern you; you can assume the core PHP implementation will be used. Either way, both implementations should act identically, otherwise you'll have a bit of a compatibility problem.

(It appears as if Guzzle defines GuzzleHttp\json_encode in its own namespace, so it's a bit weird that the IDE picks it up as identical to the non-namespaced implementation; maybe a problem of the IDE…)

  1. Object of class stdClass could not be converted to string

Well, you cannot convert an object to a string. It's undefined what the result is supposed to be. What does the string representation of an object look like…? Perhaps you want to json_encode it, which is a string representation of an object. Or var_export perhaps. Or var_dump, or any other specific function which defines a behaviour for how it will turn objects into strings.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

The simple solution would be to change this line:

// Decode the response into an object:
$dettaglioHotel = json_decode($response->getBody());

to

// Decode the response into an object:
$dettaglioHotel = json_decode($response->getBody()->getContents());

The reason being that getBody() returns an EntityBody object, and when you json_decode that object... well, I'm not really sure what happens. But I doubt it's what you would expect.

Note that using json_decode without the second parameter set to true will also give you an object. Generally if I'm going to log these I will just use Log::info(json_encode($theObject));.

EDIT: It looks like Guzzle actually does implement a version of json_decode, so your code will actually work in that case. Logging with json_encode() will fix your problem though.

Samsquanch
  • 8,866
  • 12
  • 50
  • 89
  • Well, really, `$response` appears to have a `->json()` method built-in… – deceze Jan 24 '17 at 19:13
  • @deceze it looks like, depending on the Guzzle version, [`->json()` may not be implemented](http://stackoverflow.com/questions/30530172/guzzle-6-no-more-json-method-for-responses) – Samsquanch Jan 24 '17 at 19:16
  • OK. You should probably link to a more up-to-date documentation then; I just stumbled across it in *your* link. – deceze Jan 24 '17 at 19:26