Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle page scraping errors with Simple HTML Dom Parser

Similar to Facebook, I am building an app that allows users to post a link.

The user fills out the link in an input field, and the controller returns

Title
Link
Meta description
Images (as thumbnails)

to the view.

Here is the controller code:

    $url = $this->input->post('posts_link');

    if (!empty($url)) {

        $html = file_get_html($url);

        foreach ($html->find('img') as $element) {

          $src = "";

          $src = $element->src;

            if (preg_match("/\.jp[e]?g$/i", $src)) {

            $images[] = $src;

            }
        }

        $data['posts_link'] = $url;
        $data['images']     = $images;
        $data['title']          = $html->find('title', 0)->plaintext;
        $data['meta']           = get_meta_tags($url);

The problem I'm having is when there are no images, no title, or no description (alone or in combination).

I am using codeigniter and it throws several errors on the view, which I would rather have suppressed.

Is there a best practice to suppress these errors or place empty variables in case no title/images/descriptions are returned by the DOM parser?

For example I've tried

$data['images'] = $images ? $images : '';

but it doesn't resolve my problem.

Any suggestions?

Thanks.

like image 823
pepe Avatar asked Nov 25 '22 12:11

pepe


1 Answers

You need to predefine the variables before you use them. For $images it would be

$images = array();

right after if (!empty($url)) { etc

like image 136
zerkms Avatar answered Dec 09 '22 21:12

zerkms