我绝对是新的 PHP (我来自强力打字的Java),我疯狂地试图理解为什么我不能将这个对象和某个对象转换为字符串PHP中的概念。
所以我有这个代码doLogin()方法(进入 Laravel 控制器类):
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');
}
因此,您可以在previus方法中看到我使用 Guzzle 库来执行REST调用。
我的怀疑是:
1)进入 PhpStorm (使用过的IDE),在 get()方法上传递鼠标,它告诉我返回的对象的类型为 ResponseInterface 这是一个接口,而不是具体的类类型。
究竟是什么意思?那样做:
$response = $client->get('http://localhost/Accomodation/7');
它返回一个实现 ResponseInterface 接口的通用对象? Php会自动构建类似于实现此接口的通用对象或什么?
2)将鼠标移到 json_decode()函数上,这一行:
$dettaglioHotel = json_decode($response->getBody());
IDE将消息称为"多次实施" ,并显示有这两种实现:
第一个实施: json_decode ... / vendor / guzzlehttp / guzzle / src / functions.php json_decode
第二个实施: C:/ Program Files(x86)/ JetBrains / PhpStorm 2016.3.2 / plugins / php / lib / php.jar!/stubs/standard/json.php
必须使用第一个(与 Guzzle 相关),但我不确定是否使用了正确的。
3)我最终尝试转换为字符串并在日志中打印包含响应解码的对象,这样:
$dettaglioHotel = json_decode($response->getBody());
然后:
\Log::info('response: '.(string)$dettaglioHotel);
但是当尝试转换为串口(通过(字符串)$ dettaglioHotel )时这样做会导致应用程序崩溃并且我收到此错误消息:
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
它究竟意味着什么?为什么我无法将其转换为字符串?如何查看此对象的内容?
答案 0 :(得分:0)
- ...在
醇>get()
方法上传递鼠标,它告诉我返回的对象的类型ResponseInterface
是一个接口,而不是具体的类类型
嗯,方法将返回特定类型的一些具体对象。正如你的建议,PHP不会对某些特殊对象做任何魔术。它只是意味着get()
返回的对象将遵循(implement
)ResponseInterface
。您可以将返回的对象视为拥有该接口中描述的所有方法和属性。 准确使用哪个类实例是无关紧要的。尽可能使用接口而不是具体类来保持实现的灵活性,以便在必要时交换具体类,但仍然与定义的接口兼容。 / p>
- IDE将消息称为" Multiple implementation"并表明有这两个实现
醇>
这意味着在不同的文件中执行两次相同的功能。将在运行时根据各种因素决定使用哪个版本。如果PHP安装没有安装,Guzzle最有可能带有json_decode
的后备实现。在运行时,如果需要,Guzzle会有条件地将json_decode
定义为polyfill 。这通常不必关心你;您可以假设将使用核心PHP实现。无论哪种方式,两种实现都应该完全相同,否则您会遇到一些兼容性问题。
(好像Guzzle在自己的命名空间中定义了GuzzleHttp\json_encode
,所以IDE选择它与非命名空间的实现相同有点奇怪;可能是IDE的一个问题...... )
- 类stdClass的对象无法转换为字符串
醇>
好吧,你不能将对象转换为字符串。它未定义结果应该是什么。 做什么对象的字符串表示看起来像......?也许你想要json_encode
它,它是一个对象的 字符串表示。或许var_export
。或var_dump
,或任何其他特定函数,它定义了如何将对象转换为字符串的行为。
答案 1 :(得分:0)
简单的解决方案是改变这一行:
// Decode the response into an object:
$dettaglioHotel = json_decode($response->getBody());
到
// Decode the response into an object:
$dettaglioHotel = json_decode($response->getBody()->getContents());
原因是getBody()
会返回EntityBody
个对象,而当你json_decode
该对象时......好吧,我不确定会发生什么。但我怀疑这是你所期望的。
请注意,使用json_decode
而不将第二个参数设置为true
也会为您提供一个对象。通常,如果我要记录这些,我将使用Log::info(json_encode($theObject));
。
json_decode
的版本,所以你的代码实际上会在这种情况下工作。使用json_encode()
进行记录可以解决您的问题。