View Single Post
Old 09-25-2007, 09:25 PM   #8 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by WinSrev View Post
Is it possible to somehow catch the response of file_get_contents like for example:

if everything is okay with the URL go ahead and continue, if an error occured we should try something else?

Thanks!
I'm not 100% clear on what you're asking. file_get_contents() will 'catch' the response from whatever URL you pass to it, that is the function's purpose. If you're wanting to look at the headers returned from the remote server, then after calling file_get_contents() you can access the global variable, $http_response_header which holds all of the headers from the last response.

To check for "200" like Wildhoney's CURL example you can do:
PHP Code:
<?php

// Fetch the contents of the URL
$szContents file_get_contents('http://domain.com/file.txt');

// $http_response_header contains the headers, the first header
// will always be the response code.
// E.g. "HTTP/1.1 200 OK"
if (strpos('200'$http_response_header[0]))
{
    echo 
'OK';
}
else
{
    echo 
'FAIL';
}

?>
Salathe is offline  
Reply With Quote