【问题标题】:Show Video Count Via Json通过 Json 显示视频计数
【发布时间】:2013-12-31 11:09:36
【问题描述】:

所以我一直在用imagecreatepng做一个动态签名,我使用的API是

(假设用户名是“googlechrome”)

http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json

这条线告诉我他们有 289 个视频:

{"rel":"http://gdata.youtube.com/schemas/2007#user.uploads","href":"http://gdata.youtube.com/feeds/api/users/googlechrome/uploads?v=2","countHint":289},

我不确定如何在图像上打印出来。

到目前为止我的代码。

语法:domain.com/image.php?channel=googlechrome

yt.png = http://i.imgur.com/OCRWhI6.png

<?php
$channel = $_REQUEST["channel"];
$image = ('yt.png');

$im = imagecreatefrompng($image);

$white = imagecolorallocate($im, 255, 255, 255);
$width = imagesx($im);
$height = imagesy($im);
$font = 2;

$json_output = file_get_contents('http://gdata.youtube.com/feeds/api/users/'.($channel).'?v=2&alt=json');
$json = json_decode($json_output, true);

$username = $json['entry']['yt$username']['$t'];
$view_count = $json['entry']['yt$statistics']['totalUploadViews'];
$sub_count = $json['entry']['yt$statistics']['subscriberCount'];
//$video_count = $json['entry']['DUNNO_YET']['$t'];

$UserName = ("".$username);
imagestring($im, $font, $width-190, $height-59, $UserName, $white);

$viewCount = ("".$view_count);
imagestring($im, $font, $width-190, $height-45, $viewCount, $white);

$subCount = ("".$sub_count);
imagestring($im, $font, $width-190, $height-30, $subCount, $white);

$VideoCount = ("".$video_count);
imagestring($im, $font, $width-190, $height-15, $VideoCount, $white);

//text before counts
$UNAME = ('Username:');
imagestring($im, $font, $width-278, $height-59, $UNAME, $white);
$TOTALVIEWS = ('Total Views: ');
imagestring($im, $font, $width-278, $height-45, $TOTALVIEWS, $white);
$SUBS = ('Subscribers: ');
imagestring($im, $font, $width-278, $height-30, $SUBS, $white);
$TOTALVIDEOS = ('Total Videos: ');
imagestring($im, $font, $width-278, $height-15, $TOTALVIDEOS, $white);

Header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

任何帮助表示赞赏!

【问题讨论】:

    标签: php json api youtube-api


    【解决方案1】:

    $video_count 变量应该等于:

    $video_count = $json['entry']['gd$feedLink'][6]['countHint'];
    

    但是,我不确定上传计数是否始终位于 gd$feedLink 的第 6 个索引处,因此使用类似以下内容可能是一个聪明的主意:

    $video_count = 0;
    foreach ($json['entry']['gd$feedLink'] as $feed) {
        if ($feed['rel'] == 'http://gdata.youtube.com/schemas/2007#user.uploads') {
            $video_count = $feed['countHint'];
            break;
        }
    }
    

    【讨论】:

    • 谢谢,我暂时坚持使用第 6 个索引,AFAIK 已经有一段时间了。如果它发生变化,它应该很容易编辑!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2018-02-02
    • 2013-06-05
    • 2017-07-02
    • 2013-07-31
    相关资源
    最近更新 更多