Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的YouTube PHP脚本很重,如何缓存它?_Php_Performance_Api_Caching_Youtube - Fatal编程技术网

我的YouTube PHP脚本很重,如何缓存它?

我的YouTube PHP脚本很重,如何缓存它?,php,performance,api,caching,youtube,Php,Performance,Api,Caching,Youtube,我如何在我的网站上缓存我的YouTube PHP脚本,使其每小时只请求新数据,而不是在每次页面加载时调用数据 <?php get_playlists(); function get_playlists(){ $data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2? orderby=published&max-resul

我如何在我的网站上缓存我的YouTube PHP脚本,使其每小时只请求新数据,而不是在每次页面加载时调用数据

<?php
get_playlists();

function get_playlists(){
$data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2?                      orderby=published&max-results=12");
$xml = simplexml_load_string($data);

foreach($xml->entry as $playlist){
$media = $playlist->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->thumbnail[1]->attributes();
$thumb = $attrs['url'];
$attrs = $media->group->player->attributes();
$video = $attrs['url'];
$title = substr( $media->group->title, 27);
$url = $video;

parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
$vid_Id = $my_array_of_vars['v'];

$thumbnail .= '<div style="float:left; cursor:pointer;">
<p class="crop"><a class="videobox various iframe" href="http://www.youtube.com/embed/' .         $vid_Id . '?autoplay=1&hd=1"><img src="' .$thumb . '" title="' . $title . '" width="74"         height="56"/></a></p></div>';
}print $thumbnail;}
?>


尝试使用并保存加载的播放列表

设置每小时加载电影的cron作业。电影信息存储在数据库中。避免每次下载和解析XML。对于基本的加速,这将是好的。

如果您没有访问crontab的权限,那么一个更简单的变体是将播放列表放在带有时间戳的D/B中。始终从D/B中读取这些内容,如果年龄>1小时,则刷新列表并更新表格。如果您的网站使用共享托管服务,则您将无法访问memcached。