Search code examples
phpzend-frameworkyoutube-apizend-framework3

retrieving and displaying comments from youtube API through Zend framework 3


I need to display comments from given video to be displayed inside zend framework3.

I think I am getting proper response from youtube API for listCommentThreads request as well as for listComments. My problem is I can't force zend to display it properly.

my function from service:

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

my function from controller:

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

        return $view;
    }

my comments.phtml file:

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

As for displaying I tried several different ways. Nothing seems to work for me. As it is now I am getting such error:

Notice: Undefined index: snippet in C:\\module\Youtube\view\youtube\index\comments.phtml on line 4

when i tried something like this in comments.phtml:

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

I am getting nothing and no errors. When I tried var_dump($comments) for some videos i got something like that:

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) { } } }

... it goes and goes

but when I tried to do

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

i got only NULL

what do I do wrong.


Solution

  • You are almost there.

    In your action, change:

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

    to

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

    and in your view, use:

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

    This way you are getting the replies to the 1st comment (parentId) you got.

    If you want to get all the comments of the video, do in getCommentsList:

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