Show only the latest php channel video

Asked

Viewed 729 times

0

I have this code, where when using the command /lastvideo it shows the latest video from my youtube channel, but when trying to use it only shows the youtube link, not the video

Code in Php

//CANAL
$canal = '1fHyqwbfORgIedvQ5fmWZQ';


//--------------------------------------------------------//


switch($mensagem){

    case "/lastvid":
        sendMessage($chatid, ($yt->lastVideo($canal)));
        break;

Calls this function:

class YouTube {

    public $cid;


    public function lastVideo ($cid){

    $ulast = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' .$cid. '&maxResults=1&order=date&type=video&key=xxxxxxxxxxxxxxx';
    $glast = @file_get_contents($ulast);
    $dlast = json_decode($glast, true);

    $vid = @$dlast['items'][0]['id']['videoId'];
    $vname = @$dlast['items'][0]['snippet']['title'];

    //Condition

        $ret = "Assista meu ultimo video: " .$vname. "\n http://youtu.be" .$vid;

        return $ret;
    }

}
  • You have to take the id from the last video, send it to the database, update the table, and then put it on Youtube. I recommend the following links : https://stackoverflow.com/questions/6617567/how-to-get-the-id-of-the-latest-uploaded-video-in-a-youtube-channel and https://stackoverflow.com/questions/412467/how-to-embed-youtube-videos-in-php

1 answer

0


I don’t know if it helps but I can give you the code in HTML and Javascript.

HTML

<div class="videos"></div>

Javascript (with jQuery):

$(function() {
    // Baixar URLs do feed do canal
    $.get('http://gdata.youtube.com/feeds/api/users/**nome_do_utilizador**/uploads?max-results=10&alt=json', function(result) {

        var entries = result.feed.entry;

        for(var i=0; i<entries.length; i++) {
            var url = entries[i].link[0].href;   

            // Transformar URL de vídeo em URL de embed
            // ANTES: http://www.youtube.com/watch?v=aDqExWjK49Y&algumacoisaaqui
            // DEPOIS: http://www.youtube.com/embed/aDqExWjK49Y
            url = url.replace('/watch?v=', '/embed/');
            url = url.substring(0, url.indexOf('&'));

            // Fazer append dos vídeos
            $('.videos').append('<iframe width="480" height="270" src="' + url + '" frameborder="0" allowfullscreen></iframe>');
        }
    });
});

Explanation:

Basically get downloads a video feed from a channel (change where it says username). Then, a loop is made that picks up the URL of each video, and applies a "fix" that turns it into the embed URL of Youtube. Finally, the embed HTML code is mounted, which is added to the div "videos".

The reason for the URL "fix" is that Youtube did not provide a variable with only the video ID in the feed. It would have been very convenient in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.