通过 Zend 框架 3 从 youtube API 检索和显示评论

retrieving and displaying comments from youtube API through Zend framework 3

我需要在 zend framework3 中显示来自给定视频的评论。

我认为我从 youtube API 获得了对 listCommentThreads 请求以及 listComments 的正确响应。我的问题是我无法强制 zend 正确显示它。

我的服务功能:

public function getCommentsList($id)
{
 try {
      $videoCommentThreads = $this -> youtube -> commentThreads -> listCommentThreads('snippet', array(
           'videoId' => $id,
           'textFormat' => 'plainText',
           ));
        $parentId = $videoCommentThreads[0]['id'];
        $comments = $this -> youtube -> comments -> listComments('snippet', array(
          'parentId' => $parentId,
          'textFormat' => 'plainText',
          ));
        } catch (\Exception $e) {
            die($e->getMessage());
        }

        return $comments;
    }

我的控制器功能:

public function commentsAction()
    {
        $id = $this->params()->fromRoute('id');
        $comments = $this->ytService->getCommentsList($id);
        $view = new ViewModel(['comments' => [$comments]]);
        $view->setTerminal(true);

        return $view;
    }

我的comments.phtml文件:

comments...<br />
<strong>
<?php
       echo $comments['snippet']['topLevelComment']['snippet']['textDisplay'];
?>
</strong><br/>

关于显示我尝试了几种不同的方式。似乎没有什么适合我。现在我收到这样的错误:

注意:未定义索引:第 4 行 C:\\module\Youtube\view\youtube\index\comments.phtml 中的片段

当我在 comments.phtml 中尝试这样的事情时:

comments...<BR />
<?php
foreach ($comments as $f){
echo $f['snippet']['textOriginal'];
echo "<BR />";
}
?>

我什么也没得到,也没有错误。当我为一些视频尝试 var_dump($comments) 时,我得到了类似的东西:

comments...
array(1) { [0]=> object(Google_Service_YouTube_CommentListResponse)#235 (17) { ["collection_key":protected]=> string(5) "items" ["etag"]=> string(57) ""ld9biNPKjAjgjV7EZ4EKeEGrhao/8nuwXL_uo880WPx5G4SpQo1F1Hg"" ["eventId"]=> NULL ["itemsType":protected]=> string(30) "Google_Service_YouTube_Comment" ["itemsDataType":protected]=> string(5) "array" ["kind"]=> string(27) "youtube#commentListResponse" ["nextPageToken"]=> string(112) "Q2h3Z29xZlEwS09GMkFJeUVRZ0tFQUFZeWNlUWtaclExd0lnQVNnQkVoNElCUklhVldkNFIySklYMWx5YmxSU2IycHFPV1I2ZURSQllVRkNRV2M=" ["pageInfoType":protected]=> string(31) "Google_Service_YouTube_PageInfo" ["pageInfoDataType":protected]=> string(0) "" ["tokenPaginationType":protected]=> string(38) "Google_Service_YouTube_TokenPagination" ["tokenPaginationDataType":protected]=> string(0) "" ["visitorId"]=> NULL ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["pageInfo"]=> object(Google_Service_YouTube_PageInfo)#328 (5) { ["resultsPerPage"]=> int(20) ["totalResults"]=> NULL ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } ["items"]=> array(17) { [0]=> object(Google_Service_YouTube_Comment)#245 (9) { ["etag"]=> string(57) ""ld9biNPKjAjgjV7EZ4EKeEGrhao/yMpehVGKelxJUnku4t5qoe2sMUY"" ["id"]=> string(68) "z23sdhx5esvoulnlbacdp432ahlzxlfnoual3lzfrblw03c010c.1511294181102888" ["kind"]=> string(15) "youtube#comment" ["snippetType":protected]=> string(37) "Google_Service_YouTube_CommentSnippet" ["snippetDataType":protected]=> string(0) "" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["snippet"]=> object(Google_Service_YouTube_CommentSnippet)#330 (18) { ["authorChannelId"]=> array(1) { ["value"]=> string(24) "UCAKvIEx6MoenkvZau8zcTaw" } ["authorChannelUrl"]=> string(55) "http://www.youtube.com/channel/UCAKvIEx6MoenkvZau8zcTaw" ["authorDisplayName"]=> string(7) "Adog312" ["authorProfileImageUrl"]=> string(107) "https://yt3.ggpht.com/-Vl_H7Y8JbEo/AAAAAAAAAAI/AAAAAAAAAAA/luI5nRagMa4/s28-c-k-no-mo-rj-c0xffffff/photo.jpg" ["canRate"]=> bool(true) ["channelId"]=> NULL ["likeCount"]=> int(0) ["moderationStatus"]=> NULL ["parentId"]=> string(51)"z23sdhx5esvoulnlbacdp432ahlzxlfnoual3lzfrblw03c010c" ["publishedAt"]=> string(24) "2017-11-21T19:56:21.000Z" ["textDisplay"]=> string(127) "oh, i thought that with the "add the salt" thing, it was just a joke i missed, like maybe you messed up adding the salt somehow" ["textOriginal"]=> string(127) "oh, i thought that with the "add the salt" thing, it was just a joke i missed, like maybe you messed up adding the salt somehow" ["updatedAt"]=> string(24) "2017-11-21T19:56:21.000Z" ["videoId"]=> NULL ["viewerRating"]=> string(4) "none" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } }

... 来来去去

但是当我尝试这样做时

var_dump($comments['snippet']['textDisplay']);

我只有 NULL

我做错了什么。

你快到了。

在您的操作中,更改:

$view = new ViewModel(['comments' => [$comments]]);

$view = new ViewModel(['comments' => $comments]);

在您看来,使用:

<?php
foreach ($comments as $comment){
echo $comment->getSnippet()->getAuthorDisplayName();
echo $comment->getSnippet()->getTextOriginal();
echo "<BR />";
}
?>

通过这种方式,您将获得 对第一条评论的回复 (parentId)。

如果想获取视频的所有评论,getCommentsList:

return $videoCommentThreads;

//iterate the same way.
foreach ($videoCommentThreads as $comment){
    $comment->getSnippet()->getAuthorDisplayName();
    $comment->getSnippet()->getTextOriginal();
}

感谢您的所有建议。我几乎都用过。老实说,我无法实施最后一点。但目前视图显示的是顶级评论。

我的服务功能目前看起来像这样:

public function getCommentsList($id)
    {
        try {
          $videoCommentThreads = $this -> youtube -> commentThreads -> listCommentThreads('snippet,replies', array(
           'videoId' => $id,
           'textFormat' => 'plainText',
           ));
         }
        catch (\Exception $e) {
            die($e->getMessage());
        }

        return $videoCommentThreads;
    }

我的 comments.phtml 文件目前看起来像这样

<h1 align="center">comments</h1>
<?php
foreach ($comments as $f){
  echo "<strong>".$f['snippet']['topLevelComment']['snippet']['authorDisplayName'];
  echo "</strong> <i>";
  echo $f['snippet']['topLevelComment']['snippet']['textOriginal'];
  echo "</i><HR />";
 }
?>

如有必要,将考虑对每条评论进行回复。