Why my second call on url doesn't work (Laravel,Guzzle)?

∥☆過路亽.° 提交于 2019-12-08 09:23:28

问题


This is a function where i call 2 api, from first i get client_id which i used in second url. Problem is that after i call second url my page is loading without end.

Page image

public function getDevices(){

        $route='http://localhost:8000/api/devices';

        $device= new Client();
        $answer= $device->request('GET', $route);
        $body = $answer->getBody();
        $status = 'true';
        $message = 'Data found!';
        $final= json_decode($body);

        $id_array = array();
    foreach ($finalas $item) {
        // Add each id value in your array
        $id_array[]= $item->clientId;
    }

foreach($id_array as $my_id) {
 $answer2= $client->request('GET', 'http://localhost:8080/api/devices/deviceAvailability/' . $my_id );
 $body2 = $response2->getBody();
 $final2= json_decode($body2);

 }


return view('new.home', ['clients' => $final, 'status'=> $final2]);

回答1:


I think

return view('new.home', ['clients' => $final, 'status'=> $final2])

is wrong. Because $final is decoded variable, maybe $final contains several types of variables.

In php, you can not set parameter that contains several types of variables.

Please do like that.

return view('new.home', ['clients' => $body, 'status'=> $final2]);

That's because json encoded variable is only a string.

I want your result.



来源:https://stackoverflow.com/questions/51195704/why-my-second-call-on-url-doesnt-work-laravel-guzzle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!