Welcome Guest, Not a member yet? Register   Sign In
[Solved] Getting simple_html_dom to work with CI?
#1

[eluser]zimco[/eluser]
I downloaded the simple_html_dom source code and dropped the simple_html_dom.php file into my applications libraries folder and tried running an example like the following in my controller but it errors out and the log file says:

Quote:PHP Fatal error: Call to a member function file_get_html() on a non-object

Code:
$this->load->library('simple_html_dom');

// Create DOM from URL or file
$html = $this->simple_html_dom->file_get_html($next_page);

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>';

Has anybody ever got this to work with codeigniter? If so, what changes did you have to make to simple_html_dom to get it to work? Or am i not using it, or calling it right?
#2

[eluser]davidbehler[/eluser]
As there is no simple_html_dom class in the downloaded file you would rather have to use it as a helper than as a library.
Drop the simple_html_dom.php file in your helper folder and then do it like this:
Code:
$this->load->helper('simple_html_dom');
// Create DOM from URL or file
$html = file_get_html($next_page);

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>';
Maybe that works
#3

[eluser]cideveloper[/eluser]
codeigniter libraries have a specific structure as described below

Code:
class Class_name {

      function function_name(){

      }

}

There are a lot of changes that need to be made to the simple_html_dom.php file in order for it to work as a library

1) change the file name to Simple_html_dom.php (Note Capital Letter)
2) change the simple_html_dom class to Simple_html_dom (Note Capital Letter)
3) put the file_get_html function inside the class Simple_html_dom

That will work. if you need to use the other functions that are outside the Simple_html_dom class you will need to put them inside as well.
#4

[eluser]zimco[/eluser]
@Waldmeister-the simple_html_dom class, is there (around line 481) but it's buried below another class, simple_html_dom_node, so i don't think making it a helper instead of a library will help (pardon the pun).

@progr@mmer, thanks for the 3 step tips for CI Libraries. Unfortunately, simple_html_dom still does not work, but i think it's because there were two classes in the file originally and trying to split them to work in CI and still play nice with each other (simple_html_dom calls simple_html_dom_node at one point) doesn't seem possible.

Thanks anyway guys. Looks like i'll have to find something else to do the job....
#5

[eluser]davidbehler[/eluser]
I see...well have you tried using it as a helper? I really think that might work.
#6

[eluser]zimco[/eluser]
Waldmeister is the master. Thanks for your persistence in getting me to try it as a helper, rather than a library, as my lack in understanding the subtle differences between a CI Library and CI Helper seems to have been the main problem for me getting it to work.

I got it to work by doing the following steps:

1) Drop the file simple_html_dom.php into /application/helpers folder
2) Change the name to simple_html_dom_helper.php
3) Capitalize the first letter of the two class names in the file:
around line 83 class Simple_html_dom_node
around line 481 class Simple_html_dom
4) Then remember to load the new helper in the controller with $this->load->helper('simple_html_dom')
#7

[eluser]davidbehler[/eluser]
Glad I could help Smile
#8

[eluser]XORd[/eluser]
thanks, guys!!! you saved me tons of headaches!
#9

[eluser]sophistry[/eluser]
remember to call this helper using the helper function syntax rather than the library (object) syntax:
Code:
$html = file_get_html($next_page);

NOT this:
Code:
$html = $this->simple_html_dom->file_get_html($next_page);

also, if you want to customize your http request this library allows you to take advantage of stream context support. for example, if you want to name your scraper or provide a cookie or choose a language or set a timeout:

Code:
// Create a stream
$opts = array('http'=>array('method'=>"GET",'header'=>"Accept-language: en\r\n"."Cookie: foo=bar\r\n",'user_agent'=>'simple_html_dom'));
$context = stream_context_create($opts);

$html = file_get_html('http://codeigniter.com',FALSE,$context);




Theme © iAndrew 2016 - Forum software by © MyBB