How do I use the CakePHP third party library function?
I am trying to call a third party library function in CakePHP from a controller file.
I have this in my controller file:
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");
...
}
I also have app/vendor
files simple-html-dom.php
and utils.php
.
file_get_html
is a public function in simple-html-dom.php
(and does not belong to any class). I am getting this error:
Error: Call to undefined function file_get_html()
I'm trying to find how to solve this, but I couldn't find an answer.
source to share
Try
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");
}
Make sure the file simple-html-dom.php
contains the class and then create object
this class
after loading vendor
.
because to access methods
and property
class you need to create object
this class.
You can also access method
in the same class using Self::file_get_html();
, but this is for inside class declaration
.
Additional Help
App::import('Vendor', 'example', array('file' => 'Example.php'));
$example = new Example();
in the above code i am including the provider file.
explanation
Above the code, the file Example.php
that is inside the directory will be loaded vendors/example
.
In your case, your file vendor
did not load correctly, which is why you are getting an error class not found
.
source to share