Hello,

I have this code which grabs url from a webpage, when i add multiple urls, sometimes some of the webpages does not have images in the div that i mentioned in this php code so it returns with this error : Fatal error: Call to a member function find() on a non-object in /home/liferock/public_html/testbox.co.cc/9gag/index.php on line 14

Live URL of the Code

http://testbox.co.cc/9gag/

The Code

http://snippi.com/s/cpsjxn6

Sample URLs, To Test the Script

http://9gag.com/gag/0000001
http://9gag.com/gag/0000002
http://9gag.com/gag/0000003
http://9gag.com/gag/0000004
http://9gag.com/gag/0000005
http://9gag.com/gag/0000006
http://9gag.com/gag/0000007
http://9gag.com/gag/0000008
http://9gag.com/gag/0000009
http://9gag.com/gag/0000010
http://9gag.com/gag/0000011
http://9gag.com/gag/0000012
http://9gag.com/gag/0000013
http://9gag.com/gag/0000014
http://9gag.com/gag/0000015
http://9gag.com/gag/0000016
http://9gag.com/gag/0000017
http://9gag.com/gag/0000018
http://9gag.com/gag/0000019
http://9gag.com/gag/0000020
http://9gag.com/gag/0000021
http://9gag.com/gag/0000022
http://9gag.com/gag/0000023
http://9gag.com/gag/0000024
http://9gag.com/gag/0000025
http://9gag.com/gag/0000026
http://9gag.com/gag/0000027
http://9gag.com/gag/0000028
http://9gag.com/gag/0000029
http://9gag.com/gag/0000030
http://9gag.com/gag/0000031
http://9gag.com/gag/0000032
http://9gag.com/gag/0000033
http://9gag.com/gag/0000034
http://9gag.com/gag/0000035
http://9gag.com/gag/0000036
http://9gag.com/gag/0000037
http://9gag.com/gag/0000038
http://9gag.com/gag/0000039
http://9gag.com/gag/0000040
http://9gag.com/gag/0000041
http://9gag.com/gag/0000042
http://9gag.com/gag/0000043
http://9gag.com/gag/0000044
http://9gag.com/gag/0000045
http://9gag.com/gag/0000046
http://9gag.com/gag/0000047
http://9gag.com/gag/0000048
http://9gag.com/gag/0000049
http://9gag.com/gag/0000050
http://9gag.com/gag/0000051
http://9gag.com/gag/0000052
http://9gag.com/gag/0000053
http://9gag.com/gag/0000054
http://9gag.com/gag/0000055
http://9gag.com/gag/0000056
http://9gag.com/gag/0000057
http://9gag.com/gag/0000058
http://9gag.com/gag/0000059
http://9gag.com/gag/0000060
http://9gag.com/gag/0000061
http://9gag.com/gag/0000062
http://9gag.com/gag/0000063
http://9gag.com/gag/0000064
http://9gag.com/gag/0000065
http://9gag.com/gag/0000066
http://9gag.com/gag/0000067
http://9gag.com/gag/0000068
http://9gag.com/gag/0000069
http://9gag.com/gag/0000070
http://9gag.com/gag/0000071
http://9gag.com/gag/0000072
http://9gag.com/gag/0000073
http://9gag.com/gag/0000074
http://9gag.com/gag/0000075
http://9gag.com/gag/0000076
http://9gag.com/gag/0000077
http://9gag.com/gag/0000078
http://9gag.com/gag/0000079
http://9gag.com/gag/0000080
http://9gag.com/gag/0000081
http://9gag.com/gag/0000082
http://9gag.com/gag/0000083
http://9gag.com/gag/0000084
http://9gag.com/gag/0000085
http://9gag.com/gag/0000086
http://9gag.com/gag/0000087
http://9gag.com/gag/0000088
http://9gag.com/gag/0000089
http://9gag.com/gag/0000090
http://9gag.com/gag/0000091
http://9gag.com/gag/0000092
http://9gag.com/gag/0000093
http://9gag.com/gag/0000094
http://9gag.com/gag/0000095
http://9gag.com/gag/0000096
http://9gag.com/gag/0000097
http://9gag.com/gag/0000098
http://9gag.com/gag/0000099
http://9gag.com/gag/0000100

Can anyone please suggest me the code which will fix this issue? i am quite noob to php :( only good at CSS and Photoshop.

Thanks in Advance.

Recommended Answers

You can add exception handling around it, so if it fails, it does nothing.

Jump to Post

Something like this:

foreach ($urls as $key => $url) {
    $url = trim($url);

    try {
      $html = file_get_html($url);

      $ep = $html->find('.post-container img');
      $data .= $ep[0]->src . "\n"; 
    }
    catch (Exception $e) {
        echo "Error parsing url: $url<br/>";
    }
}
Jump to Post

All 5 Replies

You can add exception handling around it, so if it fails, it does nothing.

commented: Thanks, unfortunately i am not quite sure how to do that, if not asking too much,, can you please suggest me the code to do that? Thanks again for the quick reply. +0

@pritaeas : Thanks, unfortunately i am not quite sure how to do that, if not asking too much,, can you please suggest me the code to do that? Thanks again for the quick reply.

Something like this:

foreach ($urls as $key => $url) {
    $url = trim($url);

    try {
      $html = file_get_html($url);

      $ep = $html->find('.post-container img');
      $data .= $ep[0]->src . "\n"; 
    }
    catch (Exception $e) {
        echo "Error parsing url: $url<br/>";
    }
}
commented: Thanks again :) +0

Aha. file_get_html is not a built-in PHP function. Did you download the package it belongs to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.