【问题标题】:retrieving and displaying comments from youtube API through Zend framework 3通过 Zend 框架 3 从 youtube API 检索和显示评论
【发布时间】:2017-11-21 20:18:20
【问题描述】:

我需要将给定视频中的 cmets 显示在 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;
    }

我的 cmets.phtml 文件:

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

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

注意:未定义的索引:第 4 行 C:\\module\Youtube\view\youtube\index\cmets.phtml 中的 sn-p

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

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

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

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

我做错了什么。

【问题讨论】:

    标签: php zend-framework youtube-api zend-framework3


    【解决方案1】:

    你快到了。

    在你的行动中,改变:

    $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)。

    如果要获取视频的所有cmets,在getCommentsList中做:

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

    【讨论】:

      【解决方案2】:

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

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

      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;
          }
      

      我的 cmets.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 />";
       }
      ?>
      

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

      【讨论】:

        猜你喜欢
        • 2012-06-27
        • 2013-10-22
        • 2016-05-10
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        相关资源
        最近更新 更多