在Symfony中使用PHP简单HTML DOM解析器时出错
下面是一个简单html dom如何在独立php文件中工作的基本示例 test.php:在Symfony中使用PHP简单HTML DOM解析器时出错,php,symfony1,symfony-1.4,Php,Symfony1,Symfony 1.4,下面是一个简单html dom如何在独立php文件中工作的基本示例 test.php: include ('simple_html_dom.php'); $url = "http://www.google.com"; $html = new simple_html_dom(); $html->load_file($url); print $html; 如果我用命令执行它:php test.php 它正确转储网站的html(在本例中为google.com) 现在让我们来看一个使用Syf
include ('simple_html_dom.php');
$url = "http://www.google.com";
$html = new simple_html_dom();
$html->load_file($url);
print $html;
如果我用命令执行它:php test.php
它正确转储网站的html(在本例中为google.com)
现在让我们来看一个使用SyfOffice任务的代码的基本示例:
class parserBasic extends sfBaseTask {
public function configure()
{
$this->namespace = 'parser';
$this->name = 'basic';
}
public function execute($arguments = array(), $options = array())
{
$url = "http://www.google.com";
$html = new simple_html_dom();
$html->load_file($url);
print $html;
}
}
此文件位于以下位置:/lib/task
我不需要在文件中包含库,因为它位于lib/task
文件夹下,会自动加载
我使用以下命令执行任务:php symfony parser:basic
我得到以下错误消息:
PHP Fatal error:
Call to a member function innertext() on a non-object in
/home/<username>/<appname>/lib/task/simple_html_dom.php on line 1688
PHP致命错误:
对中的非对象调用成员函数innertext()
/home///lib/task/simple\u html\u dom.php,第1688行
有什么建议吗?问题来自Symfony 事实上,如果在加载带有
simple\u html\u dom
的文件时出错,它只会返回false
例如,如果您在任务中执行此操作:
$url = "http://www.google.com";
$html = new simple_html_dom();
$res = $html->load_file($url);
if (false === $res)
{
throw new Exception("load_file failed.");
}
print $html;
你会得到一个例外。如果在加载文件时调整simple\u html\u dom
以显示en error,则在第1085行附近:
// load html from file
function load_file()
{
$args = func_get_args();
$this->load(call_user_func_array('file_get_contents', $args), true);
// Throw an error if we can't properly load the dom.
if (($error=error_get_last())!==null) {
// I added this line to see any errors
var_dump($error);
$this->clear();
return false;
}
}
你会看到:
array(4) {
["type"]=>
int(8)
["message"]=>
string(79) "ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush"
["file"]=>
string(62) "/home/.../symfony.1.4/lib/command/sfCommandApplication.class.php"
["line"]=>
int(541)
}
我在使用task时通常会遇到这个错误(实际上,这是一个通知)。问题在这里,在sfCommandApplication
中,带有ob\u end\u flush
:
/**
* Fixes php behavior if using cgi php.
*
* @see http://www.sitepoint.com/article/php-command-line-1/3
*/
protected function fixCgi()
{
// handle output buffering
@ob_end_flush();
ob_implicit_flush(true);
为了解决这个问题,我用@ob_end_flush()注释这行代码代码>。一切都很顺利。我知道,这是一个丑陋的修复,但它的工作。另一种解决方法是禁用来自PHP的通知(在PHP.ini
中),如:
// Report all errors except E_NOTICE
error_reporting = E_ALL ^ E_NOTICE