我的PHP获取YouTube视频api功能不打印任何内容

时间:2013-04-05 03:04:38

标签: php api youtube

刚刚创建一个API(作为练习)来获取我最新的YouTube视频,但没有任何内容正在打印。我是PHP的新手,刚刚开始创建自己的网站

youtube_api.inc.php

    <?php

function get_latestvideo($username)
{
    if (true || (time() - filetime("{$GLOBALS['path']}/cache/video_cache.txt")) > 3600)
    {
        $videos = array();

        $data = "http://gdata.youtube.com/feeds/api/users/{$username}/uploads?start-index=1&max-results=1&v=2&alt=json";
        foreach (json_decode(file_get_contents("$data"))->feed->entry as $video)
        {
            $url = (array)$video->link[0];

            $videos[] = array(
                'title' => $video->title->{'$t'},
                'desc' => $video->{'media$group'}->{'media$description'}->{'$t'},
                'url' => $url['href'],
            );
        }

        file_put_contents("{$GLOBALS['path']}/cache/video_cache.txt", serialize($videos));
    }else{
        $videos = unserialize(file_get_contents("{$GLOBALS['path']}/cache/video_cache.txt"));
    }
}

function get_playlists($username)
{

}

?>

init.inc.php

    <?php

$path = dirname(__FILE__);

include("youtube_api.inc.php");

?>

videos.php

<?php

header('Content-Type: text/plain');

include('init.inc.php');

print_r(get_latestvideo('thegigglesquid'));

?>

这最后一个文件应该打印$videos数组。

1 个答案:

答案 0 :(得分:2)

你永远不会从你的功能中返回任何东西。

尝试添加:

return $videos;

在函数结束时,在if() {} else {}语句之外。

function get_latestvideo($username) {    
    $videos = array();
    if (true || (time() - filetime("{$GLOBALS['path']}/cache/video_cache.txt")) > 3600) {
        // ...
    } else {
        // ...
    }
    return $videos; // <-- Important!
}
相关问题