检查对象是否有效?

时间:2014-10-04 13:14:20

标签: php amazon-web-services amazon-s3 amazon-cloudfront

我签署了我的网址,因此它们仅在一段时间内有效。

当网址无效时,我得到:

This XML file does not appear to have any style information associated with it. The     document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>Access denied</Message>
</Error>

有没有办法检查URL是否有效的对象,如果它无效我希望将用户重定向到一般错误页面。

1 个答案:

答案 0 :(得分:2)

您可以使用函数string file_get_contents(string $url)来获取URL的输出, 然后检查响应是否是您在上面指定的内容(请记住&#34;此XML文件没有任何样式......&#34;不是respnse的一部分)。

以下是一个例子:

<?php
    if (file_get_contents("http://example.com/foo/bar") === $errorResponse) {
        //There was an error
        header("Location: http://example.com/redirect/");
    }
?>

如果错误消息始终不一样,您可以使用XML解析器检查<Error>是否存在,或者只使用正则表达式。