1

我正在使用 Gdata API 来获取 Youtube 视频和评论。回复采用 XML 格式,其中包含一个数组。

对于视频 ID 和评论,XML 响应是不同的。例如,我将数组 ID 作为视频 ID 数组,对于一个 ID,数组中有一个或多个评论。

视频 ID 和评论的数组如下:

foreach ($array as $entry) 
{
    $videoid = basename($entry);
    $video[] = $videoid;

    $logger->info('Response From Youtube:videoid=>' . $videoid);
}

$this->view->videoid = $video;

$author   = array();
$content  = array();
$arraycnt = array();

foreach ($video as $id)
{
    $comment = "http://gdata.youtube.com/feeds/api/videos/".$id."/comments";
    $sxml1   = simplexml_load_file($comment);
    $entries = $sxml1->entry;

    foreach ($entries as $a)
    {
        $author[]  = $a->author->name;
        $content[] = $a->content; 
    }
}

具体观点如下:

    <table>
   <tr>
            <td>
                <?php
                for($i=0;$i<$length;$i++)
                {
                ?> 
                <embed

                width="420" height="345"
                src="http://www.youtube.com/v/<?php echo $videoid[$i];?>"
                type="application/x-shockwave-flash">
            </embed>
            <?php
            }
            ?>
        </td>

        <td>
            <?php   
            foreach($content as $cont) 
            {
            ?>
            <p>Comment:<?php echo $cont;?></p>
            <?php 
            }
            ?>
        </td>
        <td>
            <?php   
            foreach($author as $auth) 
            {
            ?>
            <p>Commented By:<?php echo $auth;?></p>
            <?php 
            }
            ?>
        </td>
    </tr>
</table>

如何在视图中显示视频和评论,例如:

视频A1 评论A1 评论A2

视频B1 评论B1 评论B2 评论B3

4

2 回答 2

0

您可以将视频的 id 保存在$author数组$content中。

foreach ($video as $id)
{
  foreach ($entries as $a)
  {
    $author[$id][]  = $a->author->name;
    $content[$id][] = $a->content; 
  }
}

因此,您可以从特定的视频 ID 获取评论的作者:

foreach ($video as $id)
{
  echo $id;
  foreach($author[$id] as $auth) {
    echo ' ', $auth;
  }
}

评论的内容也是如此。

当然,如果您只想为评论的作者和内容提供一个数组,您可以扩展此解决方案,但逻辑保持不变。

于 2013-09-04T05:57:22.990 回答
0

您在这里实际拥有的是您首先迭代视频,然后针对每个视频迭代评论。

所以你有一个嵌套迭代:第 1 级:视频,第 2 级:评论。

正如我之前评论的那样,您可以创建一个能够在多维数组中以这种方式迭代的数据的数据结构。让我们在以下示例中拍摄两个 Youtube 视频:

  1. -FRm3VPhseI:清洁代码会谈 - “全球状态和单身人士”
  2. RlfLCWKxHJ0:干净的代码说话 - 不要寻找东西!

因此,如果您想在这两个视频上创建一个包含所有评论的多维数组,您可以选择以下格式:

$videos = array(
    '-FRm3VPhseI' => array(
        'id'       => '-FRm3VPhseI',
        'title'    => 'The Clean Code Talks - "Global State and Singletons"',
        'href'     => 'http://www.youtube.com/watch?v=-FRm3VPhseI&feature=youtube_gdata',
        'comments' => array(
            array(
                'author'  => 'Nelson ThePrimate',
                'content' => 'There is a cost for r...',
            ),
            array(
                'author'  => 'dennisdegreef',
                'content' => 'That is also a global...',
            ),
            array(
                'author'  => 'MorleyCode',
                'content' => 'State is unavoidable,...',
            ),

            // ...

            array(
                'author'  => 'Jacob Jensen',
                'content' => 'I don\'t quite underst...',
            ),
            array(
                'author'  => 'unity20000',
                'content' => 'Testing is not the on...',
            ),
            array(
                'author'  => 'drummist180',
                'content' => 'Turing machine > line...',
            ),
        ),
    ),
    'RlfLCWKxHJ0' => array(
        'id'       => 'RlfLCWKxHJ0',
        'title'    => 'The Clean Code Talks - Don\'t Look For Things!',
        'href'     => 'http://www.youtube.com/watch?v=RlfLCWKxHJ0&feature=youtube_gdata',
        'comments' =>
        array(
            array(
                'author'  => 'Nikolai Paul',
                'content' => 'this guy sometimes so...',
            ),
            array(
                'author'  => 'Madrid Softwaree',
                'content' => 'Learn Selenium , QTP ...',
            ),
            array(
                'author'  => 'Roger Keulen',
                'content' => 'Di: Great as a FXCop ...',
            ),

            // ...

            array(
                'author'  => 'michaeldeng1981',
                'content' => 'if I do outsourcing p...',
            ),
            array(
                'author'  => 'Rico Lelina',
                'content' => 'How about loggers? Is...',
            ),
            array(
                'author'  => 'twistedbydsign99',
                'content' => '11:55 it should defin...',
            ),
        ),
    ),
);

它首先包含由 Youtube ID 键入/索引的所有视频,然后包含在所有评论中。要输出这个,你只需要嵌套两个 foreach 子句:

foreach ($videos as $videoId => $video)
{
    printf("%s: %s\n", $videoId, $video['title']);

    printf("  Comments (%d):\n", count($video['comments']));

    foreach ($video['comments'] as $i => $comment)
    {
        printf("    #%d %s: %s\n", $i + 1, $comment['author'], $comment['content']);
    }

    echo "\n";
}

这将创建以下输出:

-FRm3VPhseI: The Clean Code Talks - "Global State and Singletons"
  Comments (6):
    #1 Nelson ThePrimate: There is a cost for r...
    #2 dennisdegreef: That is also a global...
    #3 MorleyCode: State is unavoidable,...
    #4 Jacob Jensen: I don't quite underst...
    #5 unity20000: Testing is not the on...
    #6 drummist180: Turing machine > line...

RlfLCWKxHJ0: The Clean Code Talks - Don't Look For Things!
  Comments (6):
    #1 Nikolai Paul: this guy sometimes so...
    #2 Madrid Softwaree: Learn Selenium , QTP ...
    #3 Roger Keulen: Di: Great as a FXCop ...
    #4 michaeldeng1981: if I do outsourcing p...
    #5 Rico Lelina: How about loggers? Is...
    #6 twistedbydsign99: 11:55 it should defin...

请注意,在此输出中,每次评论的数量限制为 6,因为我减少了示例数组的评论数量。foreach现在仔细比较如何使用嵌套子句读取嵌套的多维数组的结构。外部 foreach 正在阅读视频,内部 foreach 正在阅读评论。

这个顺便说一句。与构建该数组相同,它也适用于两个嵌套迭代。为了使这更简单,我首先创建了一个缓存变量和一些辅助函数:

$gdataFetchCache = [];

$gdataFetch = function ($url) use (&$gdataFetchCache)
{
    if (!isset($gdataFetchCache[$url]))
    {
        $gdataFetchCache[$url] = simplexml_load_file($url);
    }
    return $gdataFetchCache[$url];
};

$gdataNamed = function ($pattern) use ($gdataFetch)
{
    return function ($value) use ($pattern, $gdataFetch)
    {
        return $gdataFetch(sprintf($pattern, $value));
    };
};


$ytVideo    = $gdataNamed('http://gdata.youtube.com/feeds/api/videos/%s');
$ytComments = $gdataNamed('http://gdata.youtube.com/feeds/api/videos/%s/comments');

这些函数允许在嵌套的 foreach-es 中更轻松地获取 Youtube 数据:

$videoIds = ['-FRm3VPhseI', 'RlfLCWKxHJ0'];

$videos = [];

foreach ($videoIds as $videoId)
{
    $video = $ytVideo($videoId);

    $videoArray = [
        'id'       => (string)$videoId,
        'title'    => (string)$video->title,
        'href'     => (string)$video->link['href'],
        'comments' => [],
    ];

    $videos[$videoId] = $videoArray;

    foreach ($ytComments($videoId)->entry as $comment)
    {
        $videos[$videoId]['comments'][] = [
            'author'  => (string)$comment->author->name,
            'content' => (string)$comment->content,
        ];
    }
}

如果再次仔细比较,它的结构与输出代码相同。

这是您可以读取和创建预先不知道计数的多维数组的方法。通过迭代,您可以为一个元素创建代码,但只要有元素就使用它。

这也适用于嵌套。

于 2013-09-04T07:01:22.003 回答