在Codeigniter 3.0.1中自动加载并使用简单的HTML dom外部库

时间:2015-09-29 07:34:21

标签: php codeigniter codeigniter-3

我在CodeIgniter 3.0中包含/使用外部库时遇到问题。所以这就是我到目前为止所做的:

我已将simple_html_dom.php文件放在我的应用程序/库文件夹

Including file and auto loading it

然后我用

中的这行代码自动加载它
/*
example of CI 
$autoload['libraries'] = array('user_agent' => 'ua');
*/

$autoload['libraries'] = array('simple_html_dom' => 'shd');

这是我的控制器

public function index()
    {
        $html = $this->shd->str_get_html('<html><body>Hello!</body></html>');
        var_dump($html);
        die();
        $this->load->view('parser');
    }

这给我一个错误:

A PHP Error was encountered
Severity: Error
Message: Call to undefined method simple_html_dom::str_get_html()
Filename: controllers/Parser.php
Line Number: 8

可以在link

上找到simple_html_dom的文档

对我而言,它看起来像librarie被加载,但我不能使用它的功能。 我希望有人可以提供帮助。提前谢谢!

4 个答案:

答案 0 :(得分:2)

我找到了解决方案。在检查了简单的html dom的文档后,我发现你也可以使用面向对象的方式。所以我的控制器现在看起来像这样:

$html = new simple_html_dom();
$html->load('<html><body>Hello!</body></html>');
var_dump($html);

我有结果:

object(simple_html_dom)[17]
  public 'root' => 
    object(simple_html_dom_node)[18]
      public 'nodetype' => int 5
      public 'tag' => string 'root' (length=4)
      public 'attr' => 
        array (size=0)
          empty
      public 'children' => 
        array (size=1)
          0 => 
            object(simple_html_dom_node)[19]
              ...
      public 'nodes' => 
        array (size=1)
          0 => 
            object(simple_html_dom_node)[19]
              ...
      public 'parent' => null
      public '_' => 
        array (size=2)
          0 => int -1
          1 => int 4
      public 'tag_start' => int 0
      private 'dom' => 
        &object(simple_html_dom)[17]
  public 'nodes' => 
    array (size=4)
      0 => 
        object(simple_html_dom_node)[18]
          public 'nodetype' => int 5
          public 'tag' => string 'root' (length=4)
          public 'attr' => 
            array (size=0)
              ...
          public 'children' => 
            array (size=1)
              ...
          public 'nodes' => 
            array (size=1)
              ...
          public 'parent' => null
          public '_' => 
            array (size=2)
              ...
          public 'tag_start' => int 0
          private 'dom' => 
            &object(simple_html_dom)[17]

答案 1 :(得分:1)

你可以使用

$this->load->library("simple_html_dom"); //class name should come here

并确保simple_html_dom.php班级名称为simple_html_dom

答案 2 :(得分:0)

试试这个 -

$ autoload [&#39; libraries&#39;] =数组(&#39; simple_html_dom&#39;);

答案 3 :(得分:0)

你可以这样做:

$this->load->library("simple_html_dom");
$this->simple_html_dom->your_method();