im creating a service, and in this service im getting some data from a api, it works fine, but now i need to handle some http requests, one of them is the 404, since sometimes the data im trying to retrieve is not found.
My method from my Service is:
public function getAllGamesFromDate($date = "2017-08-09", $tournament = "123")
{
$api = file_get_contents($this->url."schedules/".$date."/schedule.json?api_key=".$this->api_key);
$result = collect(json_decode($api, true))->toArray();
$data = [];
foreach ($result['events'] as $event){
if($event['id'] == $tournament){
array_push($data,$event);
}
}
return response($data);
}
When there is not data, since im not handling errors, i get this error:
ErrorException in MyService.php line 32:
file_get_contents(https://api.url...): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
What is the best way to handle this type of error?