Welcome Guest, Not a member yet? Register   Sign In
Simple DOM Helper
#1

[eluser]K.Brown[/eluser]
I'm trying to use the simple DOM helper, but I'm having some trouble.

I'm new to CI and PHP. This seems to be an error due to my server setup...

Code:
<?php

class Welcome extends Controller
{

    function __construct()
    {
        parent::Controller();
        $this->load->helper('dom');
    }

    public function index()
    {
        // Grab HTML From the URL
        $html = file_get_html('http://codeigniter.com/');

        // find all link on Codeigniter Site
        foreach($html->find('a') as $e)
        echo $e->href . '<br>';
    }
}
This is the output:

Quote:A PHP Error was encountered

Severity: Warning

Message: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration

Filename: helpers/dom_helper.php

Line Number: 40

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://codeigniter.com/) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found

Filename: helpers/dom_helper.php

Line Number: 40


What can I do to fix this?
#2

[eluser]bobbybaboon[/eluser]
I had this same problem on Media Temple. What I did to solve it was to load the page using cURL and save it as a variable and then use the str_get_html function to parse the saved variable. I don't know if this is the best solution, but it worked. If you want more a code example I could post it tonight when I'm home from work.
#3

[eluser]K.Brown[/eluser]
An example would be great!

I'm only familiar with the echo function...lol.
#4

[eluser]bobbybaboon[/eluser]
Sorry for the long delay!

Here's how I'm using cUrl and SimpleDomHelper together:
Code:
function html_example() {
        $this->load->helper('simple_html_dom');
        
        //url to be loaded through cURL
        $url = 'http://www.google.com';
        
        //initiate cURL and save data to $content
        $ch = curl_init ();
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
        $content = curl_exec ($ch);
        curl_close ($ch);
        
        //parse $content with simple_html_dom
        $html = str_get_html($content);
        
        //echo out all hyperlinks found
        foreach($html->find('a') as $element) {
                       echo $element->href;
       }
}

Hope this helps! If anyone has any suggestions for improvement, please let me know.
#5

[eluser]K.Brown[/eluser]
Awesome! Thanks a lot!

You've saved me hours!




Theme © iAndrew 2016 - Forum software by © MyBB