我无法从同一表中的2个rest api中正确打印值

时间:2018-07-19 17:45:26

标签: php laravel api

我试图从同一张表中的2个REST API打印数组。第一个可以正常工作,但是第二个数组无法正确打印。有人知道如何解决这个问题吗?

Table result

content from first array

content fro second array

http://localhost:8000/api/devices/deviceAvailability/9976 rest api json

http://localhost:8000/api/devices rest api json

Laravel控制器

//Listing clients from java application 
public function getDevices(){
$path=file_get_contents("C:\Users\Desktop\projekt1.2.2017\edit_path\edit_url.txt");

   try{
    $device= new Client();

    $answer= $client->request('GET', $path.'devices');
    $body = $answer->getBody();
    $status = 'true';
    $message = 'Data found!';
    $result= json_decode($body);
        $id_array = array();
    foreach ($result as $item) {
        // Add each id value in your array
        $id_array[]= $item->clientId;
    }

$b = array();
foreach($id_array as $my_id) {
 $answer2 = $device->request('GET', $path. 'devices/deviceAvailability/' . $my_id );
 $b[] = $answer2 ->getBody();


}

return view('devices.home', ['devices' => $result, 'status'=> $b]);
    // is thrown for 400 level errors 
}catch(ClientException $ce){
    $status = 'false';
    $message = $ce->getMessage();
    $result= [];
    //In the event of a networking error (connection timeout, DNS errors, etc.)
}catch(RequestException $re){
   $status = 'false';
   $message = $re->getMessage();
   $result= [];
}//If some error occurs
catch(Exception $e){
   $this->status = 'false';
   $this->message = $e->getMessage();
   $result= [];
}
 Session::flash('error', 'Devices list is empty, no connection !');
return view('devices.home', ['status'=>$status,'message'=>$message,'devices'=>$result]);
}

刀片php中的表

<div class="table-responsive">

        <table class="table  new_table table-striped" style="text-align: center;">

            <thead>
                <tr>

                    <th>Device Name</th>
                    <th>Device Address </th>
                    <th>Device Socket </th>
                    <th>Device Frequency Collection </th>
                    <th>Device Frequency Upload </th>
                    <th>Server Name</th>
                    <th>Server Address</th>
                    <th>Server Socket</th>
                    <th>Device avialibilty</th>
                    <th>Device Profile</th>
             @if(!Sentinel::viewer())
                 @if(Sentinel::inRole('admin'))          
                    <th>Change bouth Device Freqs</th>
                    <th>Change Device Socket and Address/th> 
                    <th>Remove Device</th>


                @endif
            @endif                      
                </tr>
            </thead>

           <tbody>

                @forelse ($devices as $device)


               <tr>

                    <td>{{ $device->deviceAllias }}</td>
                    <td>{{ $device->deviceIp }}</td>
                    <td>{{ $device->devicePort }}</td>
                    <td>{{ $device->data_Collection_Frequency }}</td>
                    <td>{{ $device->dataUpload_Frequency }}</td>
                    <td>{{ $device->serverAllias }}</td>
                    <td>{{ $device->serverIp }}</td>
                    <td>{{ $device->serverPort }}</td>
                    <td> @foreach($status as $stat)  
                   @if( $stat == "true")
                             <i class="fa fa-check-circle" style="font-size:18px;color:green">Online</i>
                        @else

                              <i class="fa fa-remove" style="font-size:18px;color:red">Offline</i>     

                  @endif 

               @endforeach</td>





         <td> 
             <a href="{{route('device.profile',  ['deviceID ' =>  $device-> deviceID])}}" class="btn btn-info pull-left" style="margin-right: 3px;">Profile</a>
         </td> 


  @if(!Sentinel::guest())
      @if(Sentinel::inRole('admin'))

     <td> 

       <a href="{{route('device.change',  ['deviceID' =>  $device-> deviceID])}}" class="btn btn-info pull-left" style="margin-right: 3px;">Frequencies Edit</a>
                 </td>  

            <td> 
       <a href="{{route('device.change.port',  ['deviceID' =>  $device-> deviceID])}}" class="btn btn-info pull-left" style="margin-right: 3px;">Port Change</a>
                 </td> 

                     <td>  
            <a href="{{route('device.delete',  ['deviceID' =>  $device-> deviceID])}}" class="btn btn-danger pull-left" style="margin-right: 3px;">Remove</a>

                 </td> 
          @endif
   @endif    

                @empty
              @if(session('error')) 
                <div class="alert alert-danger">
                    {{ session('error') }}  
                    <p>Status:  {{$status}} </p>   
                      <p> Failure explanation:  {{ $message}} </p> 
                </div>
            @endif


        @endforelse

                       </tr> 





            </tbody>





        </table>
    </div>
  @if(!Sentinel::guest())
      @if(Sentinel::inRole('admin'))  
          @if(count($clients) !== 0)


            <a href="{{ route('device.new') }}" class="btn btn-success pull-right">Register a new Device</a>

           @endif
      @endif
  @endif 
  <h3>Clients Activity</h3>
       <table id="tablica" class="table table-bordered table-striped" style="width: 100px">



            <tbody>
                      @foreach ($devices $device)    
                       <tr>
                         <td>{{ $device->deviceAllias }}</td>
                       </tr> 

                     @endforeach



            </tbody>
        </table>

1 个答案:

答案 0 :(得分:1)

尝试更改此行:

$a[] = $response2->getBody();

收件人:

$a[] = json_decode($response2->getBody());