error handling and content-type in file_get_contents with get request
<p>I have a PHP function here:</p>

<pre><code>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 &lt; 3; $attempt++) { //Try 3 times to fetch URL
        $JSON = file_get_contents($URL); //Attempt to fetch, then check Response Header
        if( !$JSON &amp;&amp; isset($http_response_header) &amp;&amp; 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
}
</code></pre>

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

<p>I have a couple main questions regarding it:</p>

<ol>
<li><p>The <code>Content-Type</code> check isn't always right, because the index isn't always 7 on a GET request.  Am I suppose to loop over the entire <code>$http_response_header</code> with <code>strstr</code> for <code>Content-Type</code> and then check it?  Seems clumsy to me.  The <a href="http://php.net/manual/en/reserved.variables.httpresponseheader.php" rel="nofollow">manual page</a> had pretty much nothing on this.  There has to be an easier way to handle that?</p></li>
<li><p>My <code>error_log</code> has lines like this on a <code>404</code>:</p></li>
</ol>

<hr>

<pre><code>[25-Oct-2012 09:02:23] PHP Warning:  file_get_contents(...) [&lt;a href='function.file-get-contents'&gt;function.file-get-contents&lt;/a&gt;]: 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
</code></pre>

<hr>

<p>I'm only interested in keeping mine (the second line) and not filling my <code>error_log</code> with both.  I found <code>@</code> could be used to suppress this on <code>file_get_contents</code>, 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?</p>
