How to call dynamic url in Guzzle with GET request


How to call dynamic url in Guzzle with GET request



I'm using Laravel and Guzzle and i want to call java spring rest api and get result from dynamic url : http://localhost:8080/api/clients/clientAvailability/{id}
.With static url (http://localhost:8080/api/clients/clientAvailability/9976 everything works great but for dynamic i don't know how to solve it.



Rest api which i want to call



Controller (function for call rest api)


$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




2 Answers
2



Use json_decode to create an array from the response body:


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 ...





Thanks for answer, I tried but now it shows nothing.
– Nenad
Jul 1 at 17:14





Have you logged out the response body to see what's in it?
– DigitalDrifter
Jul 1 at 17:22





I typed dd($body->getRequest()); and shows me a "Call to a member function getRequest() on boolean" error
– Nenad
Jul 1 at 17:29





Isn't the response what you're interested in? dd($response->getBody()) maybe?
– DigitalDrifter
Jul 1 at 17:35


dd($response->getBody())



uHhhhhhhhhhhh my mistake.Thanks



This is response body





Try $response->getBody()->getContents()
– DigitalDrifter
Jul 1 at 18:36


$response->getBody()->getContents()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?