0

刚刚创建了一个 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)
{

}

?>

初始化文件

    <?php

$path = dirname(__FILE__);

include("youtube_api.inc.php");

?>

视频.php

<?php

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

include('init.inc.php');

print_r(get_latestvideo('thegigglesquid'));

?>

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

4

1 回答 1

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!
}
于 2013-04-05T03:08:58.870 回答