3

我正在尝试从控制器文件中调用 CakePHP 中的第三方库函数。

我的控制器文件中有这个:

public function index() {
    App::import('vendor', 'simple-html-dom.php');
    App::import('vendor', 'utils.php');

    set_time_limit(0);

    $html = file_get_html("google.com");
    ...
}

我也有和app/vendor文件。simple-html-dom.phputils.php

file_get_html是一个公共函数simple-html-dom.php(并且它不属于任何类)。我最终得到这个错误:

Error: Call to undefined function file_get_html()

我一直在寻找如何解决这个问题,但我没有找到答案。

4

2 回答 2

2

我得到了我的工作。尝试这个,

App::import('Vendor', 'simple_html_dom', array('file'=>'simple_html_dom.php'));

$html = file_get_html("google.com");
于 2013-09-20T17:14:41.197 回答
1

尝试

public function index() {
    App::import('vendor', 'simple-html-dom.php');
    App::import('vendor', 'utils.php');

    set_time_limit(0);
    $SimpleHtmlDom = new SimpleHtmlDom(); // create object for html dom
    $html = $SimpleHtmlDom->file_get_html("google.com");
}

确保simple-html-dom.php文件包含类,然后create objectclass加载后需要vendor

因为要访问methodsproperty类,您需要创建object该类。

您也可以使用methodwith 在同一个类中访问,Self::file_get_html();但这是用于 inside class declaration

更多帮助

App::import('Vendor', 'example', array('file' => 'Example.php'));
$example = new Example();

在上面的代码中,我包括供应商文件。

解释

上面的代码将加载目录Example.php内的vendors/example文件。

在您的情况下,您的vendor文件未正确加载,这就是您收到class not found错误的原因。

于 2013-02-18T07:55:14.150 回答