I know there are too many similar question like in my title, but I have problem that prevents me to make it works.
I tested with three accounts and all of them in one day exceeded “request quota”. I can’t even understand that how it was happen?
I’m using this code to get 3 latest videos from youtube channel:
$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>'; } }
Script works for some hour and after that result of code disappears and I get message that tell me I reached quota.
I created 3 new accounts, created only Youtube API v3 service for a project, but I can’t even use it at all!
How to make this Youtube API V3 to work without any troubles to keep the script running?
Advertisement
Answer
seems easy to parse it from the html video list, the xpath //li/ul/li[contains(@class,'channels-content-item')]
gets you the list of videos, with a video li context node, the xpath .//*[contains(@class,'yt-lockup-title')]/a
gets you the title and .//*[@data-context-item-id]
gets you the node holding the id in the “data-context-item-id” attribute, putting it all together we get:
<?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);
which outputs:
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" }
gotdammit im bored.