PHP fatal error : Call to a member function getAttribute() on a non-object

标签 php api youtube

如果你能帮我找出我的代码有什么问题,我真的很感激..
我正在尝试从特定的 youtube channel 检索数据。
我要检索的数据是:总查看次数、订阅者和缩略图 url。

该代码正确地向我显示了总观看次数和订阅者,但缩略图代码不起作用。
我收到“ fatal error :在非对象上调用成员函数 getAttribute()”

我非常感谢您的时间和帮助,谢谢。

 <?php
function GroupViews($username) {
 $xdoc = new DomDocument;
 $xdoc->Load('http://gdata.youtube.com/feeds/api/users/ArrolladoraLimonVEVO');
 $ytStatistics = $xdoc->getElementsByTagName('statistics')->item(0);
 $totalYouTubeViews = $ytStatistics->getAttribute('totalUploadViews');
return number_format($totalYouTubeViews);
}
?>
<?php echo GroupViews(0); ?>


<?php   
    function GetytSubscribers($username) {
    $xdoc = new DomDocument;
    $xdoc->Load('http://gdata.youtube.com/feeds/api/users/'.$username.'');
    $ytStatistics = $xdoc->getElementsByTagName('statistics')->item(0);
    $totalYouTubeSubscribers = $ytStatistics->getAttribute('subscriberCount');
    return number_format($totalYouTubeSubscribers);
}
?>


<?php echo GetytSubscribers('ArrolladoraLimonVEVO'); ?>





<?php   
    function GetytThumbnail($username) {
    $xdoc = new DomDocument;
    $xdoc->Load('http://gdata.youtube.com/feeds/api/users/'.$username.'');
    $ytThumbnail = $xdoc->getElementsByTagName('media$thumbnail')->item(0);
    $thumbnail = $ytThumbnail->getAttribute('url');
    return number_format($thumbnail);
}
?>


<?php echo GetytThumbnail('ArrolladoraLimonVEVO'); ?>

最佳答案

使用该 URL 中的 XML 似乎更棘手。为什么不使用 JSON 格式?只需在您的 gdata.youtube 网址上添加 '?alt=json'。这是您的一项功能的示例

function GetytThumbnail($username) {
    $url = 'http://gdata.youtube.com/feeds/api/users/'.$username.'?alt=json';
    $json = file_get_contents($url);
    $obj = json_decode($json,true);
    return $obj['entry']['media$thumbnail']['url'];
}

您只需要更改其他函数的 return 语句。希望它有帮助:)

关于PHP fatal error : Call to a member function getAttribute() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914263/

相关文章:

c# youtube api 错误

node.js - 如何使用Youtube API获得通过videoCategoryId过滤的喜欢的视频

php - Channel.Connect.Failed 错误 NetConnection.Call.BadVersion

PHP Laravel 根据条件交替推送

rest - 如果 GET 请求的响应发生变化,是否尊重幂等性?

PHP - 404 视频未找到(视频 : PUT) YouTube API

php - 获取两个mysql选择之间的值差异

php - 显示数据库中的用户记录

api - Powershell Invoke-RestMethod

youtube - 创建自动的YouTube观看机器?