1

This is the error I got :

Fatal error: Call to a member function getAttribute() on a non-object 
in /home/a4688869/public_html/random/index.php 
on line 45

Here is a picture: http://prntscr.com/5rum9z

The function works for the first few, but breaks after a few pictures are displayed. Essentially the code generates a random html. I use cURL to get the html and then parse it with a function and then select an image from the site and then repeat the process until it I get 5 images.

My code

$original_string = '123456789abh';
$random_string = get_random_string($original_string, 6);
//Generates a random string of characters and returns the string
function get_random_string($valid_chars, $length)
{

    $random_string = ""; // start with an empty random string
    $num_valid_chars = strlen($valid_chars); // count the number of chars in the valid chars string so we know how many choices we have

    // repeat the steps until we've created a string of the right length
    for ($i = 0; $i < $length; $i++)
    {
        $random_pick = mt_rand(1, $num_valid_chars); // pick a random number from 1 up to the number of valid chars

        // take the random character out of the string of valid chars
        // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
        $random_char = $valid_chars[$random_pick-1];
        $random_string .= $random_char; // add the randomly-chosen char onto the end of our string so far
    }

    return $random_string;
}


//Parses the random website and returns the image source
function websearch($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $html = curl_exec($ch);
    curl_close($ch);

    $dom = new DOMDocument;
    @$dom->loadHTML($html); // Had to supress errors

    $img = $dom->getElementsByTagName('img')->item(1); //Get just the second picture
    $src = $img->getAttribute('src'); //Get the source of the picture

    return $src;
}

The part of the code that displays the html

for ($i = 0; $i < $perpage; $i++){
            $random_string = get_random_string($original_string, 6);
            $src = websearch('http://prntscr.com/' . $random_string);
            while( $src == "http://i.imgur.com/8tdUI8N.png"){
                $random_string = get_random_string($original_string, 6);
                $src = websearch('http://prntscr.com/' . $random_string);
            }
                ?>

<img src="<?php echo $src; ?>">
<p><a href="<?php echo $src; ?>"><?php echo $src; ?></a></p>
<?php if ($i  != $perpage - 1){ // Only display the hr if there is another picture after it ?>
<hr>
<?php }}?>
Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
Brian Bishop
  • 33
  • 1
  • 1
  • 5

1 Answers1

6

Whenever you get "non-object" errors it is because you are calling a method on a variable that is not an object.

This can be remedied by always checking your return values. It may not be very elegant, but computers are stupid and if you want your code to work then you have to always make sure that it does what you want it to do.

$img = $dom->getElementsByTagName('img')->item(1);
if ($img === null) {
    die("The image was not found!");
}

You should also get in the habit of reading the documentation for stuff that you are using (in this case the return values).

As you can see on the DOMNodelist::item page the return value, if the method failed, is null:

Return Values

The node at the indexth position in the DOMNodeList, or NULL if that is not a valid index.

Community
  • 1
  • 1
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52