简体   繁体   中英

error handling and content-type in file_get_contents with get request

I have a PHP function here:

function TryGetJSON($URL) { //Attempts to retrieve the JSON at a URL, script terminates if failure
    function LogAndDie($msg) { error_log($msg); die(); }
    for($attempt = 0; $attempt < 3; $attempt++) { //Try 3 times to fetch URL
        $JSON = file_get_contents($URL); //Attempt to fetch, then check Response Header
        if( !$JSON && isset($http_response_header) && strstr($http_response_header[0], '503'))
            continue; //503 response: server was busy, so try again
        else
            break; //$JSON is populated, must be a 200 response
    }//$JSON is always false on 404, file_get_contents always returns false on read failure

    if(isset($http_response_header)) {
        if(strstr($http_response_header[0], '503')) //If still 503, then all our attempts failed
            LogAndDie('Could not get JSON file (' . $URL . '): ' . $http_response_header[0] . ' after 3 attempts.');
        if(!strstr($http_response_header[0], '200')) //If not a 200
            LogAndDie('Could not get JSON file (' . $URL . '): ' . $http_response_header[0]);
        if(!strstr($http_response_header[7], 'application/json') ) //Check Correct Content-Type
            LogAndDie('Wrong Content Type for (' . $URL . '). Received: ' . $http_response_header[7]);
        return $JSON;
    }
    if(!$JSON) LogAndDie('Could not get JSON file (' . $URL . ').'); //Catch all
}

The gist of the function is that it die() s and writes to the error_log if it fails to retrieve a JSON from a specified URL. It reattempts 3 times in the event of 503 's.

I have a couple main questions regarding it:

  1. The Content-Type check isn't always right, because the index isn't always 7 on a GET request. Am I suppose to loop over the entire $http_response_header with strstr for Content-Type and then check it? Seems clumsy to me. The manual page had pretty much nothing on this. There has to be an easier way to handle that?

  2. My error_log has lines like this on a 404 :


[25-Oct-2012 09:02:23] PHP Warning:  file_get_contents(...) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in ... on line 8
[25-Oct-2012 09:02:23] Could not get JSON file (...): HTTP/1.1 404 Not Found

I'm only interested in keeping mine (the second line) and not filling my error_log with both. I found @ could be used to suppress this on file_get_contents , but that might suppress other warnings I might need to know about that I cannot predict. Is there a way to just suppress that specific warning in this function?

According to this question, you can get the content-type header from the $_SERVER["CONTENT_TYPE"] superglobal (I haven't checked, so I can't be sure). EDIT: Have now checked and it appears to be available only for POST requests.

for the file_get_contents question, you may as well suppress the warning if you don't want it, and you can explicitly test if it's returning false.

Just a note; Defining a function within a function is not a good idea - you'll get an error if you call the TryGetJSON function twice within the same script, as you can't define a function with the same name as one already defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM