[英]error handling and content-type in file_get_contents with get request
I have a PHP function here: 我在这里有一个PHP函数:
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. 该函数的要旨是,如果die()
未能从指定的URL检索JSON,则将其写入error_log
。 It reattempts 3 times in the event of 503
's. 如果出现503
它将重新尝试3次。
I have a couple main questions regarding it: 我对此有两个主要问题:
The Content-Type
check isn't always right, because the index isn't always 7 on a GET request. Content-Type
检查并不总是正确的,因为在GET请求中索引并不总是7。 Am I suppose to loop over the entire $http_response_header
with strstr
for Content-Type
and then check it? 我是否想使用strstr
遍历整个$http_response_header
以获得Content-Type
,然后进行检查? Seems clumsy to me. 在我看来笨拙。 The manual page had pretty much nothing on this. 手册页对此几乎没有任何内容。 There has to be an easier way to handle that? 必须有一个更简单的方法来解决这个问题?
My error_log
has lines like this on a 404
: 我的error_log
在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. 我只想保留我的内容(第二行),而不用两者都填写我的error_log
。 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. 我发现@
可以用来在file_get_contents
上禁止显示,但这可能会禁止我可能需要了解的其他无法预测的警告。 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). 根据此问题,您可以从$_SERVER["CONTENT_TYPE"]
超全局变量获取content-type
标头(我尚未检查,因此无法确定)。 EDIT: Have now checked and it appears to be available only for POST requests. 编辑:现在已检查,它似乎仅可用于POST请求。
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. 对于file_get_contents
问题,如果不想使用警告,也可以取消警告,并且可以显式测试警告是否返回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. 在函数中定义一个函数不是一个好主意-如果在同一脚本中两次调用TryGetJSON
函数, TryGetJSON
出错,因为您无法使用与已定义的相同名称来定义函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.