file_get_contents when url doesn't exist

前端 未结 8 2240
刺人心
刺人心 2020-12-01 02:37

I\'m using file_get_contents() to access a URL.

file_get_contents(\'http://somenotrealurl.com/notrealpage\');

If the URL is not real, it r

相关标签:
8条回答
  • 2020-12-01 03:00

    With such commands in PHP, you can prefix them with an @ to suppress such warnings.

    @file_get_contents('http://somenotrealurl.com/notrealpage');
    

    file_get_contents() returns FALSE if a failure occurs, so if you check the returned result against that then you can handle the failure

    $pageDocument = @file_get_contents('http://somenotrealurl.com/notrealpage');
    
    if ($pageDocument === false) {
        // Handle error
    }
    
    0 讨论(0)
  • 2020-12-01 03:02

    To avoid double requests as commented by Orbling on the answer of ynh you could combine their answers. If you get a valid response in the first place, use that. If not find out what the problem was (if needed).

    $urlToGet = 'http://somenotrealurl.com/notrealpage';
    $pageDocument = @file_get_contents($urlToGet);
    if ($pageDocument === false) {
         $headers = get_headers($urlToGet);
         $responseCode = substr($headers[0], 9, 3);
         // Handle errors based on response code
         if ($responseCode == '404') {
             //do something, page is missing
         }
         // Etc.
    } else {
         // Use $pageDocument, echo or whatever you are doing
    }
    
    0 讨论(0)
  • 2020-12-01 03:04

    While file_get_contents is very terse and convenient, I tend to favour the Curl library for better control. Here's an example.

    function fetchUrl($uri) {
        $handle = curl_init();
    
        curl_setopt($handle, CURLOPT_URL, $uri);
        curl_setopt($handle, CURLOPT_POST, false);
        curl_setopt($handle, CURLOPT_BINARYTRANSFER, false);
        curl_setopt($handle, CURLOPT_HEADER, true);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
    
        $response = curl_exec($handle);
        $hlength  = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        $body     = substr($response, $hlength);
    
        // If HTTP response is not 200, throw exception
        if ($httpCode != 200) {
            throw new Exception($httpCode);
        }
    
        return $body;
    }
    
    $url = 'http://some.host.com/path/to/doc';
    
    try {
        $response = fetchUrl($url);
    } catch (Exception $e) {
        error_log('Fetch URL failed: ' . $e->getMessage() . ' for ' . $url);
    }
    
    0 讨论(0)
  • 2020-12-01 03:12

    You need to check the HTTP response code:

    function get_http_response_code($url) {
        $headers = get_headers($url);
        return substr($headers[0], 9, 3);
    }
    if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "200"){
        echo "error";
    }else{
        file_get_contents('http://somenotrealurl.com/notrealpage');
    }
    
    0 讨论(0)
  • 2020-12-01 03:12

    You may add 'ignore_errors' => true to options:

    $options = array(
      'http' => array(
        'ignore_errors' => true,
        'header' => "Content-Type: application/json\r\n"
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents('http://example.com', false, $context);
    

    In that case you will be able to read a response from the server.

    0 讨论(0)
  • 2020-12-01 03:15

    Each time you call file_get_contents with an http wrapper, a variable in local scope is created: $http_response_header

    This variable contains all HTTP headers. This method is better over get_headers() function since only one request is executed.

    Note: 2 different requests can end differently. For example, get_headers() will return 503 and file_get_contents() would return 200. And you would get proper output but would not use it due to 503 error in get_headers() call.

    function getUrl($url) {
        $content = file_get_contents($url);
        // you can add some code to extract/parse response number from first header. 
        // For example from "HTTP/1.1 200 OK" string.
        return array(
                'headers' => $http_response_header,
                'content' => $content
            );
    }
    
    // Handle 40x and 50x errors
    $response = getUrl("http://example.com/secret-message");
    if ($response['content'] === FALSE)
        echo $response['headers'][0];   // HTTP/1.1 401 Unauthorized
    else
        echo $response['content'];
    

    This aproach also alows you to have track of few request headers stored in different variables since if you use file_get_contents() $http_response_header is overwritten in local scope.

    0 讨论(0)
提交回复
热议问题