开发者

How to handle page scraping errors with Simple HTML Dom Parser

开发者 https://www.devze.com 2023-03-14 18:28 出处:网络
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

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.


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

$images = array();

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


if (array_key_exists('images', $data)) {
    // display the image
    }
else
{

    //set a default image, etc
}

or something like isset


you can suppress errors by using the @ symbol. i.e.

 @     $data['images']     = $images;
0

精彩评论

暂无评论...
验证码 换一张
取 消