get_meta_tags() 和重定向限制的错误处理?
我在脚本中使用 get_meta_tags() ,并且在某些 URL 上失败(作为示例)...
警告: get_meta_tags(http://www.kodak.com/) [function .get-meta-tags]:无法打开流:达到重定向限制...
是否可以跳过任何引发错误的结果?或者我应该使用 @get_meta_tags() 代替?
function getMeta()
{
$tags = get_meta_tags($this->link); //INSERT INTEGRITY CHECK HERE?
$keywords = $tags['keywords'];
if(count($keywords))
{
preg_match_all('/(?<=^|,)\s*((?:[^\s,]+\s*){1,4})(?=\s*(?:,|$))/', $keywords, $m);
$this->keywords = array_slice($m[1], 0, 15);
}
}
I'm using get_meta_tags() in a script and on certain URLs it fails out with (as an example)...
Warning: get_meta_tags(http://www.kodak.com/) [function.get-meta-tags]: failed to open stream: Redirection limit reached...
Is it possible to just skip over any result that throws an error? Or should I just use @get_meta_tags() instead?
function getMeta()
{
$tags = get_meta_tags($this->link); //INSERT INTEGRITY CHECK HERE?
$keywords = $tags['keywords'];
if(count($keywords))
{
preg_match_all('/(?<=^|,)\s*((?:[^\s,]+\s*){1,4})(?=\s*(?:,|$))/', $keywords, $m);
$this->keywords = array_slice($m[1], 0, 15);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用错误抑制运算符 -
@
字符< /a> - 直接在调用get_meta_tags()
之前。这具有仅关闭该行的所有错误报告的效果,但它通常被认为是一种不好的做法,只有在其他选项都用尽时才使用。您对此的第一反应应该是尽可能尝试将规范 URL 传递给
get_meta_tags()
- 即,您应该尝试在重定向链的末尾传递 URL:例如,链接到 <代码>http://www.php.net/manual/en/language.types.array.php而不是http://php.net/array
。但是,如果$this-link
来自您无法控制的来源,那么使用错误抑制可能是您最好的选择:您可以按如下方式使用它:
这在功能上与此相同:
您将需要更改其余代码以处理不包含元标记数组的
$tags
变量。You could use the error suppression operator - the
@
character - directly before the call toget_meta_tags()
. This has the effect of turning all error reporting off for that line only, but it's generally regarded as a bad practice, only to be used when you other options are all exhausted.Your first reaction to this should be to try to pass a canonical URL to
get_meta_tags()
where possible - i.e. you should try to pass the URL at the end of the redirection chain: for example, link tohttp://www.php.net/manual/en/language.types.array.php
rather thanhttp://php.net/array
. If$this-link
comes from a source out of your control, however, using error suppression might be your best bet:You can use it as follows:
This is functionally the same as this:
You'll need to change the rest of your code to deal with the
$tags
variable not containing an array of meta tags.我不同意使用错误抑制,但有一些例外它确实派上用场。
请注意,
@
错误抑制器将抑制所有 错误,而不仅仅是超出重定向限制。 查看文档I don't agree with using error suppressing, but there are a few exceptions it does come in handy.
Be warned that the
@
error suppressor will suppress all errors, not just exceeded redirect limit. See docs这个可以抑制所有错误,但正是您正在寻找的东西
This one suppress all errors, but does exactly thing you're looking for