gpt4 book ai didi

simplepie - 如何让 SimplePie 在无效提要上正常失败?

转载 作者:行者123 更新时间:2023-12-01 03:56:36 25 4
gpt4 key购买 nike

如果我提供的其中一个提要不可用或无效(由于提要提供者端的服务器问题),我正试图让 SimplePie 正常失败

我得到的代码是这样的:

$this->feed= new SimplePie();
// Set which feed to process.
$this->feed->set_feed_url('http://my_feed_goes_here'); // Bogus
$this->feed->handle_content_type();

// Run SimplePie.
$this->feed->init();

问题是,如果 feed_url 被证明是无效的,我会在遇到 $this->feed->init(); 时立即收到以下错误。
Fatal error: Call to undefined method DOMElement::getLineNo() 

我已经查看了文档,但看不到任何有关验证的信息。我确实看到了有关错误检查的页面( http://simplepie.org/wiki/reference/simplepie/error ),但只有在 URL 完全无效且无法加载时才真正有效。在 URL 返回 404 或其他无效提要的情况下,$feed->error 为空。

SimplePie 中是不是有一些机制可以让我查看是否得到了有效的反馈,这样如果我没有得到有效的反馈,我就可以优雅地失败?

最佳答案

在 SimplePie 1.3.1 中,->init()如果无法读取或解析 URL,将返回 false,因此您可以这样做:

if (! $feed->init()) {
// log your error, "return false" or handle the failure some other way
}

基于我对 simplepie\library\SimplePie.php 的阅读,它不会产生任何异常,这就是 Try/Catch 不起作用的原因。

关于simplepie - 如何让 SimplePie 在无效提要上正常失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16804454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com