【发布时间】: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