即时通讯创建一个服务,并在这个服务即时通讯从一个API获取一些数据,它工作正常,但现在我需要处理一些HTTP请求,其中之一是404,因为有时数据即时尝试检索没有找到。处理Http请求找不到
我从我服务的方法是:
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);
}
当没有数据,因为我不是处理错误,我得到这个错误:
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
什么是处理这个问题的最好办法错误类型?
可能重复[file \ _get \ _contents()如何修复错误“无法打开流”,“没有这样的文件”](https://stackoverflow.com/questions/20562368/file-get-contents-如何修复错误失败打开流没有这样的文件) –