I'm downloading a file from the web with file_get_contents
.
Sometimes I get 503 Service Unavailable or 404 Not Found.
Warning: file_get_contents(http://somewhereoverinternets.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in somesourcefile.php on line 20
How can I get this error code - 503 ? 404, 200? To make the process for these cases.
Try curl instead:
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
if(!curl_errno($ch)){
return $data;
}else{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
}