2017-07-16 80 views
-1

該腳本的加載時間很長。緩慢的PHP腳本YoutubeAPI

如果我刷新頁面,則需要3.5秒來加載此腳本。

這是什麼原因造成的?

<?php 
//Instagram Getter 

// use this instagram access token generator http://instagram.pixelunion.net/ 
$access_token=""; 
$photo_count=9; 

$json_instalink="https://api.instagram.com/v1/users/self/media/recent/?access_token=$access_token&count=$photo_count"; 
$jsoninsta = file_get_contents($json_instalink); 
$objinsta = json_decode($jsoninsta); 

//Link zu den Bildern 
$imagelink1 = $objinsta->data[0]->link; 
$imagelink2 = $objinsta->data[1]->link; 
$imagelink3 = $objinsta->data[2]->link; 
$imagelink4 = $objinsta->data[3]->link; 
$imagelink5 = $objinsta->data[4]->link; 
$imagelink6 = $objinsta->data[5]->link; 
$imagelink7 = $objinsta->data[6]->link; 
$imagelink8 = $objinsta->data[7]->link; 
$imagelink9 = $objinsta->data[8]->link; 


//Das Bild an sich 
$profilelink1 = $objinsta->data[0]->images->standard_resolution->url; 
$profilelink2 = $objinsta->data[1]->images->standard_resolution->url; 
$profilelink3 = $objinsta->data[2]->images->standard_resolution->url; 
$profilelink4 = $objinsta->data[3]->images->standard_resolution->url; 
$profilelink5 = $objinsta->data[4]->images->standard_resolution->url; 
$profilelink6 = $objinsta->data[5]->images->standard_resolution->url; 
$profilelink7 = $objinsta->data[6]->images->standard_resolution->url; 
$profilelink8 = $objinsta->data[7]->images->standard_resolution->url; 
$profilelink9 = $objinsta->data[8]->images->standard_resolution->url; 

//Channel and API Key 
$channel_id = ''; 
$api_key = ''; 



//Channel Statistics 
$json_statsurl="https://www.googleapis.com/youtube/v3/channels?part=statistics&id=$channel_id&key=$api_key"; 
$jsonstats = file_get_contents($json_statsurl); 
$listFromYouTube=json_decode($jsonstats); 
$subscriberCount = $listFromYouTube->items[0]->statistics->subscriberCount; 



//Last Video 
$json_lastvidurl="https://www.googleapis.com/youtube/v3/search?key=$api_key&channelId=$channel_id&part=id&order=date&maxResults=1"; 
$jsonlastvid = file_get_contents($json_lastvidurl); 
$listFromYouTube=json_decode($jsonlastvid); 
$lastvidid = $listFromYouTube->items[0]->id->videoId; 

//Last Video Views 
$json_lastvidstatsurl="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$lastvidid&key=$api_key"; 
$jsonlastvidstats = file_get_contents($json_lastvidstatsurl); 
$listFromYouTube=json_decode($jsonlastvidstats); 
$lastvidviews = $listFromYouTube->items[0]->statistics->viewCount; 

//Last Video Published Date 
$json_lastvidinfourl="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$lastvidid&key=$api_key"; 
$jsonlastvidinfo = file_get_contents($json_lastvidinfourl); 
$listFromYouTube=json_decode($jsonlastvidinfo); 
$lastvidtitle = $listFromYouTube->items[0]->snippet->title; 
$lastvidpublished = $listFromYouTube->items[0]->snippet->publishedAt; 


//Playlist Videos 9 

$json_9vidurl="https://www.googleapis.com/youtube/v3/search?key=$api_key&channelId=$channel_id&part=snippet,id&order=date&maxResults=10"; 
$json9vid = file_get_contents($json_9vidurl); 
$listFromYouTube=json_decode($json9vid); 
$vid0=$listFromYouTube->items[1]->id->videoId; 
$vid1=$listFromYouTube->items[2]->id->videoId; 
$vid2=$listFromYouTube->items[3]->id->videoId; 
$vid3=$listFromYouTube->items[4]->id->videoId; 
$vid4=$listFromYouTube->items[5]->id->videoId; 
$vid5=$listFromYouTube->items[6]->id->videoId; 
$vid6=$listFromYouTube->items[7]->id->videoId; 
$vid7=$listFromYouTube->items[8]->id->videoId; 
$vid8=$listFromYouTube->items[9]->id->videoId; 


$watchlink="http://undercover-gaming.de/youtube/video/"; 

// TITLE PART 
$vid0t=$listFromYouTube->items[1]->snippet->title; 
$vid1t=$listFromYouTube->items[2]->snippet->title; 
$vid2t=$listFromYouTube->items[3]->snippet->title; 
$vid3t=$listFromYouTube->items[4]->snippet->title; 
$vid4t=$listFromYouTube->items[5]->snippet->title; 
$vid5t=$listFromYouTube->items[6]->snippet->title; 
$vid6t=$listFromYouTube->items[7]->snippet->title; 
$vid7t=$listFromYouTube->items[8]->snippet->title; 
$vid8t=$listFromYouTube->items[9]->snippet->title; 

// PUBLISH PART 
$json_vid0url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid0&key=$api_key"; 
$jsonlastvid0 = file_get_contents($json_vid0url); 
$listFromYouTube=json_decode($jsonlastvid0); 
$vid0published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid1url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid1&key=$api_key"; 
$jsonlastvid1 = file_get_contents($json_vid1url); 
$listFromYouTube=json_decode($jsonlastvid1); 
$vid1published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid2url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid2&key=$api_key"; 
$jsonlastvid2 = file_get_contents($json_vid2url); 
$listFromYouTube=json_decode($jsonlastvid2); 
$vid2published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid3url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid3&key=$api_key"; 
$jsonlastvid3 = file_get_contents($json_vid3url); 
$listFromYouTube=json_decode($jsonlastvid3); 
$vid3published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid4url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid4&key=$api_key"; 
$jsonlastvid4 = file_get_contents($json_vid4url); 
$listFromYouTube=json_decode($jsonlastvid4); 
$vid4published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid5url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid5&key=$api_key"; 
$jsonlastvid5 = file_get_contents($json_vid5url); 
$listFromYouTube=json_decode($jsonlastvid5); 
$vid5published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid6url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid6&key=$api_key"; 
$jsonlastvid6 = file_get_contents($json_vid6url); 
$listFromYouTube=json_decode($jsonlastvid6); 
$vid6published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid7url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid7&key=$api_key"; 
$jsonlastvid7 = file_get_contents($json_vid7url); 
$listFromYouTube=json_decode($jsonlastvid7); 
$vid7published = $listFromYouTube->items[0]->snippet->publishedAt; 

$json_vid8url="https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vid8&key=$api_key"; 
$jsonlastvid8 = file_get_contents($json_vid8url); 
$listFromYouTube=json_decode($jsonlastvid8); 
$vid8published = $listFromYouTube->items[0]->snippet->publishedAt; 

//VIEW PART 0-8 
$json_viewgetter="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid0&key=$api_key"; 
$jsonplaylistgetter = file_get_contents($json_viewgetter); 
$listFromYouTube=json_decode($jsonplaylistgetter); 
$vid0views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter1="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid1&key=$api_key"; 
$jsonplaylistgetter1 = file_get_contents($json_viewgetter1); 
$listFromYouTube=json_decode($jsonplaylistgetter1); 
$vid1views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter2="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid2&key=$api_key"; 
$jsonplaylistgetter2 = file_get_contents($json_viewgetter2); 
$listFromYouTube=json_decode($jsonplaylistgetter2); 
$vid2views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter3="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid3&key=$api_key"; 
$jsonplaylistgetter3 = file_get_contents($json_viewgetter3); 
$listFromYouTube=json_decode($jsonplaylistgetter); 
$vid3views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter4="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid4&key=$api_key"; 
$jsonplaylistgetter4 = file_get_contents($json_viewgetter4); 
$listFromYouTube=json_decode($jsonplaylistgetter4); 
$vid4views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter5="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid5&key=$api_key"; 
$jsonplaylistgetter5 = file_get_contents($json_viewgetter5); 
$listFromYouTube=json_decode($jsonplaylistgetter5); 
$vid5views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter6="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid6&key=$api_key"; 
$jsonplaylistgetter6 = file_get_contents($json_viewgetter6); 
$listFromYouTube=json_decode($jsonplaylistgetter6); 
$vid6views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter7="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid7&key=$api_key"; 
$jsonplaylistgetter7 = file_get_contents($json_viewgetter7); 
$listFromYouTube=json_decode($jsonplaylistgetter7); 
$vid7views=$listFromYouTube->items[0]->statistics->viewCount; 

$json_viewgetter8="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$vid8&key=$api_key"; 
$jsonplaylistgetter8 = file_get_contents($json_viewgetter8); 
$listFromYouTube=json_decode($jsonplaylistgetter8); 
$vid8views=$listFromYouTube->items[0]->statistics->viewCount; 




//Last Video Thumbnails 

$images = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$lastvidid."?v=2&alt=json"), true); 
$images = $images['entry']['media$group']['media$thumbnail']; 
$image = $images[count($images)-4]['url']; 

$maxurl = "http://i.ytimg.com/vi/".$lastvidid."/maxresdefault.jpg"; 
$vidurl0 = "http://i.ytimg.com/vi/".$vid0."/maxresdefault.jpg"; 
$vidurl1 = "http://i.ytimg.com/vi/".$vid1."/maxresdefault.jpg"; 
$vidurl2 = "http://i.ytimg.com/vi/".$vid2."/maxresdefault.jpg"; 
$vidurl3 = "http://i.ytimg.com/vi/".$vid3."/maxresdefault.jpg"; 
$vidurl4 = "http://i.ytimg.com/vi/".$vid4."/maxresdefault.jpg"; 
$vidurl5 = "http://i.ytimg.com/vi/".$vid5."/maxresdefault.jpg"; 
$vidurl6 = "http://i.ytimg.com/vi/".$vid6."/maxresdefault.jpg"; 
$vidurl7 = "http://i.ytimg.com/vi/".$vid7."/maxresdefault.jpg"; 
$vidurl8 = "http://i.ytimg.com/vi/".$vid8."/maxresdefault.jpg"; 
$max = get_headers($maxurl); 

if (substr($max[0], 9, 3) !== '404') { 
    $image = $maxurl; 
} 


?> 
+1

那麼你有25遠程file_get_contents調用。您可以通過緩存結果來減少加載時間。 –

+0

或者我應該嘗試更改爲jquery代碼...但從來沒有使用jquery的YouTube API,但我googleing它。感謝您的編輯。 – Sodaki

+0

@DaveChen我得到了問題..我現在把someids在一個和它的快速...問題是instagram api我的服務器需要時間與insta api連接..如果我使用instagram的緩存選項我需要w8的1小時,直到一個新的圖片更新.....緩存是好的...我如何檢查我的服務器之間使用nginx和instagram api請求之間的響應時間? – Sodaki

回答

0

我改變了一下我的代碼,並沒有那麼多的file_get_contents。 但我發現Instagram API真的很慢。所以,如果我整合了我的網頁加載非常緩慢的instagram API。如果我使用jquery代碼,它的工作速度很快,但是,每個人都可以查看代碼並查看腳本括號,那就是我不想要的。

<?php 
//Instagram Getter 

// use this instagram access token generator http://instagram.pixelunion.net/ 
$access_token=""; 
$photo_count=9; 

$json_instalink="https://api.instagram.com/v1/users/self/media/recent/?access_token=$access_token&count=$photo_count"; 
$jsoninsta = file_get_contents($json_instalink); 
$objinsta = json_decode($jsoninsta); 

//Link zu den Bildern 
$imagelink1 = $objinsta->data[0]->link; 
$imagelink2 = $objinsta->data[1]->link; 
$imagelink3 = $objinsta->data[2]->link; 
$imagelink4 = $objinsta->data[3]->link; 
$imagelink5 = $objinsta->data[4]->link; 
$imagelink6 = $objinsta->data[5]->link; 
$imagelink7 = $objinsta->data[6]->link; 
$imagelink8 = $objinsta->data[7]->link; 
$imagelink9 = $objinsta->data[8]->link; 


//Das Bild an sich 
$profilelink1 = $objinsta->data[0]->images->standard_resolution->url; 
$profilelink2 = $objinsta->data[1]->images->standard_resolution->url; 
$profilelink3 = $objinsta->data[2]->images->standard_resolution->url; 
$profilelink4 = $objinsta->data[3]->images->standard_resolution->url; 
$profilelink5 = $objinsta->data[4]->images->standard_resolution->url; 
$profilelink6 = $objinsta->data[5]->images->standard_resolution->url; 
$profilelink7 = $objinsta->data[6]->images->standard_resolution->url; 
$profilelink8 = $objinsta->data[7]->images->standard_resolution->url; 
$profilelink9 = $objinsta->data[8]->images->standard_resolution->url; 

//Channel and API Key 
$channel_id = ''; 
$api_key = ''; 

//Channel Statistics 
$json_statsurl="https://www.googleapis.com/youtube/v3/channels?part=statistics&id=$channel_id&key=$api_key"; 
$jsonstats = file_get_contents($json_statsurl); 
$listFromYouTube=json_decode($jsonstats); 
$subscriberCount = $listFromYouTube->items[0]->statistics->subscriberCount; 

//Last Video ID/Title/Published/Thumbnails 
$json_lastvidurl="https://www.googleapis.com/youtube/v3/search?key=$api_key&channelId=$channel_id&part=snippet,id&order=date&maxResults=10"; 
$jsonlastvid = file_get_contents($json_lastvidurl); 
$listFromYouTube=json_decode($jsonlastvid); 

$lastvidid = $listFromYouTube->items[0]->id->videoId; 
$lastvidtitle = $listFromYouTube->items[0]->snippet->title; 
$lastvidpublished = $listFromYouTube->items[0]->snippet->publishedAt; 
$maxurl = "http://i.ytimg.com/vi/".$lastvidid."/maxresdefault.jpg"; 


//Playlist Videos 9 ID/Title/Published/Thumbnails 
$vid0=$listFromYouTube->items[1]->id->videoId; 
$vid1=$listFromYouTube->items[2]->id->videoId; 
$vid2=$listFromYouTube->items[3]->id->videoId; 
$vid3=$listFromYouTube->items[4]->id->videoId; 
$vid4=$listFromYouTube->items[5]->id->videoId; 
$vid5=$listFromYouTube->items[6]->id->videoId; 
$vid6=$listFromYouTube->items[7]->id->videoId; 
$vid7=$listFromYouTube->items[8]->id->videoId; 
$vid8=$listFromYouTube->items[9]->id->videoId; 

$vid0t=$listFromYouTube->items[1]->snippet->title; 
$vid1t=$listFromYouTube->items[2]->snippet->title; 
$vid2t=$listFromYouTube->items[3]->snippet->title; 
$vid3t=$listFromYouTube->items[4]->snippet->title; 
$vid4t=$listFromYouTube->items[5]->snippet->title; 
$vid5t=$listFromYouTube->items[6]->snippet->title; 
$vid6t=$listFromYouTube->items[7]->snippet->title; 
$vid7t=$listFromYouTube->items[8]->snippet->title; 
$vid8t=$listFromYouTube->items[9]->snippet->title; 

$vid0published = $listFromYouTube->items[1]->snippet->publishedAt; 
$vid1published = $listFromYouTube->items[2]->snippet->publishedAt; 
$vid2published = $listFromYouTube->items[3]->snippet->publishedAt; 
$vid3published = $listFromYouTube->items[4]->snippet->publishedAt; 
$vid4published = $listFromYouTube->items[5]->snippet->publishedAt; 
$vid5published = $listFromYouTube->items[6]->snippet->publishedAt; 
$vid6published = $listFromYouTube->items[7]->snippet->publishedAt; 
$vid7published = $listFromYouTube->items[8]->snippet->publishedAt; 
$vid8published = $listFromYouTube->items[9]->snippet->publishedAt; 

$vidurl0 = "http://i.ytimg.com/vi/".$vid0."/maxresdefault.jpg"; 
$vidurl1 = "http://i.ytimg.com/vi/".$vid1."/maxresdefault.jpg"; 
$vidurl2 = "http://i.ytimg.com/vi/".$vid2."/maxresdefault.jpg"; 
$vidurl3 = "http://i.ytimg.com/vi/".$vid3."/maxresdefault.jpg"; 
$vidurl4 = "http://i.ytimg.com/vi/".$vid4."/maxresdefault.jpg"; 
$vidurl5 = "http://i.ytimg.com/vi/".$vid5."/maxresdefault.jpg"; 
$vidurl6 = "http://i.ytimg.com/vi/".$vid6."/maxresdefault.jpg"; 
$vidurl7 = "http://i.ytimg.com/vi/".$vid7."/maxresdefault.jpg"; 
$vidurl8 = "http://i.ytimg.com/vi/".$vid8."/maxresdefault.jpg"; 

//Last Video Views 
$json_lastvidstatsurl="https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$lastvidid,$vid0,$vid1,$vid2,$vid3,$vid4,$vid5,$vid6,$vid7,$vid8&key=$api_key"; 
$jsonlastvidstats = file_get_contents($json_lastvidstatsurl); 
$listFromYouTube=json_decode($jsonlastvidstats); 
$lastvidviews = $listFromYouTube->items[0]->statistics->viewCount; 
$vid0views = $listFromYouTube->items[1]->statistics->viewCount; 
$vid1views = $listFromYouTube->items[2]->statistics->viewCount; 
$vid2views = $listFromYouTube->items[3]->statistics->viewCount; 
$vid3views = $listFromYouTube->items[4]->statistics->viewCount; 
$vid4views = $listFromYouTube->items[5]->statistics->viewCount; 
$vid5views = $listFromYouTube->items[6]->statistics->viewCount; 
$vid6views = $listFromYouTube->items[7]->statistics->viewCount; 
$vid7views = $listFromYouTube->items[8]->statistics->viewCount; 
$vid8views = $listFromYouTube->items[9]->statistics->viewCount; 

//Watchlink 
$watchlink="http://undercover-gaming.de/youtube/video/"; 


?>