Php 如何从youtube频道获取3个最新视频?
我知道在我的标题中有太多类似的问题,但我有一个问题阻碍了我的工作 我测试了三个帐户,一天之内所有帐户都超过了“请求配额”。我甚至不明白这是怎么发生的 我正在使用此代码从youtube频道获取3个最新视频:Php 如何从youtube频道获取3个最新视频?,php,curl,youtube,youtube-api,youtube-data-api,Php,Curl,Youtube,Youtube Api,Youtube Data Api,我知道在我的标题中有太多类似的问题,但我有一个问题阻碍了我的工作 我测试了三个帐户,一天之内所有帐户都超过了“请求配额”。我甚至不明白这是怎么发生的 我正在使用此代码从youtube频道获取3个最新视频: $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&a
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
foreach($videoList->items as $item){
//Embed video
if(isset($item->id->videoId)){
echo '<div class="video">
<iframe width="100%" class="youtube-video" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>';
}
}
$videoList=json解码(文件获取内容('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=“.$channelID.&maxResults=”.$maxResults.&key=”.$API_key.”);
foreach($videoList->items as$item){
//嵌入视频
如果(设置($item->id->videoId)){
回声'
';
}
}
脚本工作了一个小时,代码结果消失后,我收到一条消息,告诉我我达到了配额
我创建了3个新帐户,只为一个项目创建了Youtube API v3服务,但我甚至不能使用它
如何使这个Youtube API V3能够正常工作,从而保持脚本运行?似乎很容易从html视频列表中解析它,xpath
//li/ul/li[contains(@class,'channels-content-item')]
通过视频li上下文节点,xpath/*[contains(@class,'yt-lockup-title'))获取视频列表/a
获取标题,//*[@data-context-item-id]
获取在“data-context-item-id”属性中保存id的节点,将其全部放在一起,我们得到:
<?php
declare(strict_types=1);
// $html = file_get_contents("html.html");
$html = file_get_contents("https://www.youtube.com/user/Tobuscus/videos?view=0&sort=dd&flow=grid");
$domd = @DOMDocument::loadHTML($html);
$xp = new DOMXPath($domd);
$videoList = $xp->query("//li/ul/li[contains(@class,'channels-content-item')]");
$parsed = array();
for ($i = 0; $i < 3; ++$i) {
$video = $videoList[$i];
$title = trim($xp->query(".//*[contains(@class,'yt-lockup-title')]/a", $video)->item(0)->textContent);
$id = $xp->query(".//*[@data-context-item-id]", $video)->item(0)->getAttribute("data-context-item-id");
$parsed[$id] = $title;
}
var_dump($parsed);
gotdammit我很无聊。试试使用这个插件。它对我来说很好。每次加载页面时代码都会运行吗?缓存结果以节省配额成本。@johnh10,你能告诉我如何缓存它吗?嗨!我正在wordpress中尝试你的代码,但是我在
declare(strict\u types=1)中遇到了一个错误
所以如果我删除它,我就没有var\u dump
@Jose的输出,严格的类型部分是不需要的,我甚至不确定它是否与wordpress兼容;但是,如果您没有var_dump输出,这表明您得到了一个异常,请检查您的php错误日志,这可能是一个错误,解释了为什么您没有得到输出
array(3) {
["HKlOgarmkcY"]=>
string(44) "I haven't Played this Game in a LONG Time..."
["YZac_eyIa0c"]=>
string(37) "Something is happening in two days..."
["5gY_v-T9kAM"]=>
string(14) "Dear Algorithm"
}