在非对象上调用成员函数getAttribute()

Bri*_*hop 1 php curl

这是我得到的错误

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

这是一张图片:http : //prntscr.com/5rum9z

该功能适用​​于前几张照片,但在显示几张照片后会中断。本质上,代码会生成随机的html。我使用cURL来获取html,然后使用函数对其进行解析,然后从站点中选择图像,然后重复该过程,直到获得5张图像为止。

我的密码

$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;
}
Run Code Online (Sandbox Code Playgroud)

显示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 }}?>
Run Code Online (Sandbox Code Playgroud)

Sve*_*sen 6

每当您遇到“非对象”错误时,这是​​因为您正在对不是对象的变量调用方法。

可以通过始终检查您的返回值来解决此问题。它可能不是很优雅,但是计算机很愚蠢,如果您想让代码正常工作,则必须始终确保它能够执行您想要的工作。

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

您还应该养成阅读文档的目的,以了解正在使用的东西(在这种情况下为返回值)。

如您在DOMNodelist::item页面上看到的,如果方法失败,则返回值是null

返回值

DOMNodeList中位于索引位置的节点,如果不是有效索引,则为NULL。