douying9296
2019-04-29 11:28为什么这个file_get_contents()有时工作,有时不工作?
I am building a small interface to show livestreamed video from a YouTube channel to a webpage. I got the API key, I set it up and it worked. But then I couldn't access certain information from the json I got back from the YouTube server.
I tried a few things, fixing whatever breaks on the go, but I realised that sometimes it works and sometimes it doesn't... I searched other threads but I can't understand anymore what's happening.
So the questions are:
How to use file_get_contents_url() to get info about a Youtube channel every time without HTTP response failure?
Why is my testing using all the units I have as a quota from Google's API? (It runs out of units in a 50 refreshes or so)
My code
<?php
header("Access-Control-Allow-Origin: *");
# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);
$API_KEY = 'API KEY HERE';
$ChannelID = ' CHANNEL ID HERE';
$channelInfo = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='.$ChannelID.'&type=video&eventType=live&key='.$API_KEY;
$extractInfo = file_get_contents_url($channelInfo);
$extractInfo = str_replace('},]',"}]",$extractInfo);
$showInfo = json_decode($extractInfo, true);
if($showInfo['pageInfo']['totalResults'] === 0){
echo 'Users channel is Offline';
}else{
echo 'Users channel is LIVE!';
}
$videoId = $extractInfo['items'][0]['id']["videoId"];
if ($videoId = 0) {
echo "<h2>No live stream yet.</h2>";
} else {
echo "</h2>$videoId</h2>";
}
?>
What I need is:
- to access my YouTube channel,
- get the content as json,
- turn it into a php array,
- where I get the videoId of the livestreamed video
- and display it.
When I echo $extractInfo
I get
{
"kind": "youtube#searchListResponse",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/yOWNawoTMP4atq-Ylgqt-1puBAQ\"",
"regionCode": "NL",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": []
} 0
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- Apache服务器中未定义File_get_contents错误
- json
- apache
- php
- 1个回答
- 如何修复'file_get_content'无法打开流
- laravel
- php
- 1个回答
- 请问下php中 file_get_contents ( "php://input" ); 这个函数这样写,读取的是文件路径下的文件?
- php
- 2个回答
- PHP从file_get_contents获取特定标签[重复]
- json
- php
- 1个回答
- 有没有办法用php file_get_contents绕过403错误?
- php
- 2个回答
换一换