从 Youtube Channel 获取视频链接,从 Urls 中提取视频 ID 并存储在数组中 [英] Get Video Links from Youtube Channel, extract video IDs from Urls and store them in an array

查看:58
本文介绍了从 Youtube Channel 获取视频链接,从 Urls 中提取视频 ID 并存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在本网站上的第一个问题.我是 php 的新手,但我遵循了建议,并尽可能地弄脏了我的手.不幸的是,现在我对我正在尝试创建的这个简单的 Youtube 应用程序感到有些困惑.

This is my first question on this site. I'm new to php but i've followed advice and gotten my hands as filthy with it as possible. Unfortunately now I'm a little stumped with this simple Youtube app i'm trying to create.

我知道有几个相关的问题,但我还没有找到解决我的问题的全面解决方案.

I know there are a several related questions out there, but i haven't yet found a comprehensive solution to my problem.

无论如何,我想要做的是获取 youtube 频道中视频的 url,提取视频 ID 并创建一个数组,然后我可以将其传递给一些很酷的客户端内容的 javascript 函数.

Anyway, what I'm trying to do is get the urls of videos in a youtube channel, extract the video ID and create an array that i can then pass off to a javascript function for some cool client-side stuff.

到目前为止,这是我的代码.我很确定我的问题与数组与字符串以及方法内外的变量有关.在任何情况下,我的 array_map 函数都不起作用并且 showFullFeed() 函数只返回一个值而不是链接数组.

Here is my code so far. I'm pretty sure that my issue relates to arrays vs strings and variables in and out of methods. In any case, my array_map function is not working and the showFullFeed() function is only returning one value instead of the array of links.

非常感谢任何帮助.干杯

Any help is much appreaciated. Cheers

 

class ChannelFeed {

class ChannelFeed {

函数__construct($用户名){$this->username=$username;$this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/favorites';$this->feed=simplexml_load_file($url);}

function __construct($username) { $this->username=$username; $this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/favorites'; $this->feed=simplexml_load_file($url); }

公共函数 getYTid() {

public function getYTid() {

$ytURL = $this->feed->entry->link['href'];

$ytURL = $this->feed->entry->link['href'];

$ytvIDlen = 11;//这是 YouTube 的视频 ID 的长度

$ytvIDlen = 11; // This is the length of YouTube's video IDs

//ID 字符串在v="之后开始,通常在//"youtube.com/watch?"在网址中$idStarts = strpos($ytURL, "?v=");

// The ID string starts after "v=", which is usually right after // "youtube.com/watch?" in the URL $idStarts = strpos($ytURL, "?v=");

//如果v="不在?"之后(不太可能,但我喜欢保留我的//基础覆盖),它将在&"之后:if($idStarts === FALSE)$idStarts = strpos($ytURL, "&v=");//如果仍然为 FALSE,则 URL 没有 vid IDif($idStarts === FALSE)die("未找到 YouTube 视频 ID.请仔细检查您的 URL.");

// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my // bases covered), it will be after an "&": if($idStarts === FALSE) $idStarts = strpos($ytURL, "&v="); // If still FALSE, URL doesn't have a vid ID if($idStarts === FALSE) die("YouTube video ID not found. Please double-check your URL.");

//偏移起始位置以匹配ID字符串的开头$idStarts +=3;

// Offset the start location to match the beginning of the ID string $idStarts +=3;

//获取ID字符串并返回$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
返回 $ytvID;
}公共函数 showFullFeed(){foreach($this->feed->entry as $video){返回 $vidarray[] = $video->link['href'];}}};$youtube = new ChannelFeed('用户名');$vids = $youtube->showFullFeed();$vidIDs = array_map(getYTid(),$vids);

// Get the ID string and return it $ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
} public function showFullFeed() { foreach($this->feed->entry as $video){ return $vidarray[] = $video->link['href']; } } }; $youtube = new ChannelFeed('username'); $vids = $youtube->showFullFeed(); $vidIDs = array_map(getYTid(),$vids);

?>

推荐答案

这有效.不是我将流从收藏夹更改为频道已上传的视频,因此您可能需要将其更改回来.

This works. Not I changed the stream from favorites to videos that have been uploaded by the channel, so you may want to change that back.

首先,youtube 函数的代码,我把它放在一个单独的 php 文件中并导入它,只是为了让事情更整洁.

First, the code for the youtube functions, I put this in a separate php file and imported it, just to keep things tidier.

<?php
  class ChannelFeed
  {
    function __construct( $username )
    {
        $this->username=$username;
        $this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/uploads';
        $this->feed=simplexml_load_file($url);
    }

    public function showFullFeed()
    { 
        $vidarray = array();

        foreach($this->feed->entry as $video)
        {
            $vidURL = (string)$video->link['href'];
            $vidarray[] = $vidURL;
        }

        return $vidarray;
}
}

function getYouTubeID($vidURL)
{
    $ytvIDlen = 11; // This is the length of YouTube's video IDs

    // The ID string starts after "v=", which is usually right after 
    // "youtube.com/watch?" in the URL
    $idStarts = strpos($vidURL, "?v=");

    // In case the "v=" is NOT right after the "?" (not likely, but I like to keep my 
    // bases covered), it will be after an "&":
    if($idStarts === FALSE)
        $idStarts = strpos($vidURL, "&v=");

    // If still FALSE, URL doesn't have a vid ID
    if($idStarts === FALSE)
        die("YouTube video ID not found. Please double-check your URL.");

    // Offset the start location to match the beginning of the ID string
    $idStarts +=3;

    // Get the ID string and return it
    $ytvID = substr($vidURL, $idStarts, $ytvIDlen);    

    return $ytvID;   
}
?>

接下来是调用它的主文件中的代码

Next is the code in the main file that calls it

require_once('youtube_channelfeed.php');

$youtube = new ChannelFeed('username');
$vids = $youtube->showFullFeed();
$vidIDs = array_map("getYouTubeID",$vids);

这会获取 ID 并使用数组映射函数将它们存储在数组中.

This gets the IDs and stores them in an array using the array map function.

感谢您发布代码,我正在寻找可以执行此操作的方法,因此它对我帮助很大.

Thanks for posting the code, I was looking for something to do what this does, so it's helped me out a lot.

这篇关于从 Youtube Channel 获取视频链接,从 Urls 中提取视频 ID 并存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆