Reputation: 483
I am using 3rd party api in my laravel 5.2 project.
I have installed guzzle to do so.
public function getmovie(Request $request)
{
$client= new Client();
$movieurl="http://api.cinemalytics.com/v1/movie/title/?value=madaari&auth_token=<token>";
$movie=json_decode($client->request('GET',$movieurl)->getBody(),true);
return $movie[0]['OriginalTitle'];
}
Above API code in controller function returns correct data, no problem but when I return view and send $movie to it then in blade I am facing problem accessing the values of $movie
public function getmovie(Request $request)
{
$client= new Client();
$movieurl="http://api.cinemalytics.com/v1/movie/title/?value=madaari&auth_token=<token>";
$movie=json_decode($client->request('GET',$movieurl)->getBody(),true);
return view('admin.loadmovie',compact('movie'));
}
In views I am accessing it as
<div class="form-group margin-top-20">
<label class="control-label col-md-3">Movie Title
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="text" class="form-control" name="movie_title" value="{{ $movie[0]['OriginalTitle'] }}">
</div>
</div>
</div>
It's giving me error message i.e Trying to get property of non-object
Upvotes: 0
Views: 428
Reputation: 9749
First of all you should check if the API returns data, not assume you'll always have a successful response.
You get Trying to get property of non-object because in json_decode()
the second parameter is converting the JSON to associative array. So you either remove the true or you access it as array.
Upvotes: 0