Reputation: 5
Just been crating an API (as an exercise) to get my latest YouTube video and nothing is being printed. I'm very new to PHP and have just started to create myself a website
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'));
?>
This last file is supposed to print the $videos
array.
Upvotes: 0
Views: 172
Reputation: 59709
You never return anything from your function.
Try adding:
return $videos;
At the end of your function, outside of the if() {} else {}
statement.
function get_latestvideo($username) {
$videos = array();
if (true || (time() - filetime("{$GLOBALS['path']}/cache/video_cache.txt")) > 3600) {
// ...
} else {
// ...
}
return $videos; // <-- Important!
}
Upvotes: 2