PHP simplexml_load_file赶上403年

时间:2022-02-26 00:13:40

I am using the following PHP:

我使用的PHP如下:

$xml = simplexml_load_file($request_url) or die("url not loading");

I use:

我使用:

$status = $xml->Response->Status->code;

To check the status of the response. 200 bening everything is ok, carry on.

检查响应的状态。一切都好,继续。

However if I get a 403 access denied error, how do I catch this in PHP so I can return a user friendly warning?

但是,如果我得到一个403访问拒绝错误,我如何在PHP中捕获它,以便返回用户友好的警告?

2 个解决方案

#1


8  

To retrieve the HTTP response code from a call to simplexml_load_file(), the only way I know is to use PHP's little known $http_response_header. This variable is automagically created as an array containing each response header separately, everytime you make a HTTP request through the HTTP wrapper. In other words, everytime you use simplexml_load_file() or file_get_contents() with a URL that starts with "http://"

要从对simplexml_load_file()的调用中检索HTTP响应代码,我知道的惟一方法是使用PHP鲜为人知的$http_response_header。这个变量是作为一个数组自动创建的,它包含每个响应头,每次您通过HTTP包装器发出HTTP请求。换句话说,每次使用simplexml_load_file()或file_get_contents()时,URL都以“http://”开头

You can inspect its content with a print_r() such as

您可以使用print_r()来检查它的内容。

$xml = @simplexml_load_file($request_url);
print_r($http_response_header);

In your case, though, you might want to retrieve the XML separately with file_get_contents() then, test whether you got a 4xx response, then if not, pass the body to simplexml_load_string(). For instance:

但是,在您的示例中,您可能希望使用file_get_contents()单独检索XML,然后测试是否得到4xx响应,如果没有,则将主体传递给simplexml_load_string()。例如:

$response = @file_get_contents($request_url);
if (preg_match('#^HTTP/... 4..#', $http_response_header[0]))
{
    // received a 4xx response
}

$xml = simplexml_load_string($response);

#2


0  

You'll have to use something like the cURL module or the HTTP module to fetch the file, then use the functionality provided by them to detect an HTTP error, then pass the string from them into simplexml_load_string.

您将不得不使用cURL模块或HTTP模块来获取文件,然后使用它们提供的功能来检测HTTP错误,然后将字符串从它们传递到simplexml_load_string。

#1


8  

To retrieve the HTTP response code from a call to simplexml_load_file(), the only way I know is to use PHP's little known $http_response_header. This variable is automagically created as an array containing each response header separately, everytime you make a HTTP request through the HTTP wrapper. In other words, everytime you use simplexml_load_file() or file_get_contents() with a URL that starts with "http://"

要从对simplexml_load_file()的调用中检索HTTP响应代码,我知道的惟一方法是使用PHP鲜为人知的$http_response_header。这个变量是作为一个数组自动创建的,它包含每个响应头,每次您通过HTTP包装器发出HTTP请求。换句话说,每次使用simplexml_load_file()或file_get_contents()时,URL都以“http://”开头

You can inspect its content with a print_r() such as

您可以使用print_r()来检查它的内容。

$xml = @simplexml_load_file($request_url);
print_r($http_response_header);

In your case, though, you might want to retrieve the XML separately with file_get_contents() then, test whether you got a 4xx response, then if not, pass the body to simplexml_load_string(). For instance:

但是,在您的示例中,您可能希望使用file_get_contents()单独检索XML,然后测试是否得到4xx响应,如果没有,则将主体传递给simplexml_load_string()。例如:

$response = @file_get_contents($request_url);
if (preg_match('#^HTTP/... 4..#', $http_response_header[0]))
{
    // received a 4xx response
}

$xml = simplexml_load_string($response);

#2


0  

You'll have to use something like the cURL module or the HTTP module to fetch the file, then use the functionality provided by them to detect an HTTP error, then pass the string from them into simplexml_load_string.

您将不得不使用cURL模块或HTTP模块来获取文件,然后使用它们提供的功能来检测HTTP错误,然后将字符串从它们传递到simplexml_load_string。