我正在使用Laravel和Guzzle,我想调用java spring rest api并从动态url获取结果: http:// localhost:8080 / api / clients / clientAvailability / {id}。使用静态url(http:// localhost) :8080 / api / clients / clientAvailability / 9976一切都很好,但对于动态我不知道如何解决它。
控制器
$url='http://localhost:8080/api/clients/clientAvailability/9976';
try{
$client = new Client();
$response = $client->request('GET', $url);
$body = $response->getBody();
$status = 'true';
$message = 'Data found!';
return view('chart.clientProfile', ['clients' => $body]);
// is thrown for 400 level errors
}catch(ClientException $ce){
$status = 'false';
$message = $ce->getMessage();
$data = [];
//In the event of a networking error (connection timeout, DNS errors, etc.)
}catch(RequestException $re){
$status = 'false';
$message = $re->getMessage();
$data = [];
}//If some error occurs
catch(Exception $e){
$this->status = 'false';
$this->message = $e->getMessage();
$data = [];
}
return view('chart.clientProfile', ['status'=>$status,'message'=>$message,'clients'=>$data]);
}
index.blade.php
@extends('layouts.app')
@section('content')
{{ $clients }}
@endsection
发布于 2018-07-24 14:53:36
使用json_decode
若要从响应体创建数组,请执行以下操作:
$body = json_decode($response->getBody(), true);
if ((json_last_error() === JSON_ERROR_NONE) && is_array($body)) {
return view('chart.clientProfile', ['clients' => $body['clients']]);
}
// handle json decode error ...
发布于 2018-07-24 16:11:56
https://stackoverflow.com/questions/-100005682
复制相似问题