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

Php 对非对象调用成员函数getAttribute(),php,curl,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 这是一张照片: 该功能适用于前几张图片,但在显示几张图片后会中断。基本上,代码生成一个随机html。我使用cURL获取html,然后用函数解析它,然后从站点选择一个图像,然后重复这个过程,直到得到5个图像 我的代码 $

这是我得到的错误

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

该功能适用于前几张图片,但在显示几张图片后会中断。基本上,代码生成一个随机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;
}
$original_string='123456789abh';
$random\u string=get\u random\u string($original\u string,6);
//生成一个随机字符串并返回该字符串
函数get\u random\u string($valid\u chars,$length)
{
$random_string=”“;//以空的随机字符串开始
$num_valid_chars=strlen($valid_chars);//计算有效字符字符串中的字符数,以便知道有多少个选项
//重复这些步骤,直到我们创建了正确长度的字符串
对于($i=0;$i<$length;$i++)
{
$random\u pick=mt\u rand(1,$num\u valid\u chars);//从1到有效字符数中选择一个随机数
//从有效字符字符串中取出随机字符
//从$random_pick中减去1,因为字符串的索引从0开始,我们从1开始拾取
$random_char=$valid_chars[$random_pick-1];
$random\u string.=$random\u char;//将随机选择的字符添加到字符串的末尾
}
返回$random_字符串;
}
//解析随机网站并返回图像源
函数websearch($url){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$html=curl\u exec($ch);
卷曲关闭($ch);
$dom=新的DOMDocument;
@$dom->loadHTML($html);//必须消除错误
$img=$dom->getElementsByTagName('img')->item(1);//只获取第二张图片
$src=$img->getAttribute('src');//获取图片的来源
返回$src;
}
显示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 }}?>
for($i=0;$i<$perpage;$i++){
$random\u string=get\u random\u string($original\u string,6);
$src=websearch('http://prntscr.com/“.$random_字符串);
而($src==”http://i.imgur.com/8tdUI8N.png"){
$random\u string=get\u random\u string($original\u string,6);
$src=websearch('http://prntscr.com/“.$random_字符串);
}
?>
">


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

这可以通过经常检查返回值来解决。它可能不是很优雅,但计算机很愚蠢,如果你想让代码正常工作,那么你必须始终确保它做你想做的事情

$img = $dom->getElementsByTagName('img')->item(1);
if ($img === null) {
    die("The image was not found!");
}
您还应该养成阅读文档的习惯,了解您正在使用的内容(在本例中是返回值)

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

返回值

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

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

这可以通过经常检查返回值来解决。它可能不是很优雅,但计算机很愚蠢,如果你想让代码正常工作,那么你必须始终确保它做你想做的事情

$img = $dom->getElementsByTagName('img')->item(1);
if ($img === null) {
    die("The image was not found!");
}
您还应该养成阅读文档的习惯,了解您正在使用的内容(在本例中是返回值)

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

返回值

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


谢谢你,它奏效了,是的,我应该习惯于阅读文档。非常感谢。谢谢你,它奏效了,是的,我应该习惯于阅读文档。非常感谢。