Jump to content

need quick help with img url grabber code, getting error on urls that does not..


Recommended Posts

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.

<?php

if(!empty($_POST['go']))
{
    $i=1;
$urls=(explode("\n", $_POST['urls']));
include('simple_html_dom.php');
$data='';

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



$ep=$html->find('.post-container img');
$data.=$ep[0]->src."\n"; 
//echo $ep[0]->src."<br/>";

}
}

?>

<form method="post">
    <table>
        <tr>
            <td>URLS</td>
            <td><textarea name="urls" style="width: 503px; height: 227px;"><?php echo (!empty($_POST['urls'])) ?  $_POST['urls'] : "" ; ?></textarea></td>
        </tr>

         <tr>
            <td>OUT PUT</td>
            <td><textarea name="outputs" style="width: 503px; height: 227px;"><?php echo $data; ?></textarea></td>
        </tr>

         <tr>
            <td>.</td>
            <td><input type="submit" name="go" /></td>
        </tr>
    </table>

</form>

 

Give us the php code that gives you this errror.

So this call

$html=file_get_html($url);

is failing for (at least) some url, resulting in $html being false (which I learned from looking at the code, which is the *magic of open source*). Therefore, you need to handle the condition where $html is false.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.