Youtube API显示“回复” - PHP

时间:2017-08-16 05:38:21

标签: php json

如何在youtube评论中显示回复。

这是代码

$videoid = 'UKdZU9Db6fk';
$apikey = 'API KEY';

$json = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?maxResults=100&part=snippet%2Creplies&videoId='.$videoid.'&key='.$apikey);
$ytdata = json_decode($json);

foreach ($ytdata->items as $item) {
echo "Author: ". $item->snippet->topLevelComment->snippet->authorDisplayName;
echo "Comment: ". $item->snippet->topLevelComment->snippet->textDisplay;

    foreach ($ytdata->items->replies as $iteminner) {
        echo "Reply: ". $iteminner->comments[0]->textDisplay;
    }
}

它显示评论,但是对特定评论的回复是我不能做的,如果有评论有自己的回复,那该怎么办。

我一直在网上搜索,大多数问题都是一样的,他们也有同样的问题。

我正在尝试将foreach添加到foreach中,但结果是

"Trying to get property of non-object"
"Invalid argument supplied for foreach() "

以下是xml数据的方法

{
 "kind": "youtube#commentThreadListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/MtlA2Cs5Rg8ie3kOpkRRrgrPHBM\"",
 "pageInfo": {
  "totalResults": 55,
  "resultsPerPage": 100
 },
 "items": [
  {
   "kind": "youtube#commentThread",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/rb1QsCtA9ouCv8sdiCPD4eUUebk\"",
   "id": "z23qclra1kawfhpre04t1aokgi54x0wn3upimgxpq2rork0h00410",
   "snippet": {
    "videoId": "UKdZU9Db6fk",
    "topLevelComment": {
     "kind": "youtube#comment",
     "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/cXxMy-_o92OrTFG-Vc7Hah2uVv8\"",
     "id": "z23qclra1kawfhpre04t1aokgi54x0wn3upimgxpq2rork0h00410",
     "snippet": {
      "authorDisplayName": "Manufacturing Intellect",
      "authorProfileImageUrl": "https://yt3.ggpht.com/-6LRgMkOjOK4/AAAAAAAAAAI/AAAAAAAAAAA/oDbx6JuPunE/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
      "authorChannelUrl": "http://www.youtube.com/channel/UCmHu591mWNj_zSaSuYVwsaQ",
      "authorChannelId": {
       "value": "UCmHu591mWNj_zSaSuYVwsaQ"
      },
      "videoId": "UKdZU9Db6fk",
      "textDisplay": "Check out the DFW Playlist for more: \u003ca href=\"https://www.youtube.com/playlist?list=PLIZqvqbtz9I0eQPOx46sU6p6xaz469vxk\"\u003ehttps://www.youtube.com/playlist?list=PLIZqvqbtz9I0eQPOx46sU6p6xaz469vxk\u003c/a\u003e\u003cbr /\u003eSee more on the Authors Playlist: \u003ca href=\"https://www.youtube.com/playlist?list=PLIZqvqbtz9I30kDK7RrKXxtLK9WxA33-T\"\u003ehttps://www.youtube.com/playlist?list=PLIZqvqbtz9I30kDK7RrKXxtLK9WxA33-T\u003c/a\u003e",
      "textOriginal": "Check out the DFW Playlist for more: https://www.youtube.com/playlist?list=PLIZqvqbtz9I0eQPOx46sU6p6xaz469vxk\nSee more on the Authors Playlist: https://www.youtube.com/playlist?list=PLIZqvqbtz9I30kDK7RrKXxtLK9WxA33-T",
      "canRate": true,
      "viewerRating": "none",
      "likeCount": 0,
      "publishedAt": "2017-08-10T22:11:40.000Z",
      "updatedAt": "2017-08-10T22:11:40.000Z"
     }
    },
    "canReply": true,
    "totalReplyCount": 0,
    "isPublic": true
   }
  },
  {
   "kind": "youtube#commentThread",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HtioehinkoBHz5S21dB_30Ld-3Y\"",
   "id": "z22pzvoxummny3lhw04t1aokgu2xwtnob5vdry5hqrhdbk0h00410",
   "snippet": {
    "videoId": "UKdZU9Db6fk",
    "topLevelComment": {
     "kind": "youtube#comment",
     "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/DzQufnBVZXLFDm7NKEOhj244IG8\"",
     "id": "z22pzvoxummny3lhw04t1aokgu2xwtnob5vdry5hqrhdbk0h00410",
     "snippet": {
      "authorDisplayName": "dagnyatl",
      "authorProfileImageUrl": "https://yt3.ggpht.com/-ayrANb9OdFo/AAAAAAAAAAI/AAAAAAAAAAA/-i9QNDQzUuk/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
      "authorChannelUrl": "http://www.youtube.com/channel/UCcEnnlcB76MWZ82hDnk_g6A",
      "authorChannelId": {
       "value": "UCcEnnlcB76MWZ82hDnk_g6A"
      },
      "videoId": "UKdZU9Db6fk",
      "textDisplay": "For being such an intelligent, well-educated guy, David Foster Wallace, seems ignorant of the fact that our society is governed at the top by Luciferians which have directed every avenue our country is going.  And having attended elitist  Harvard for a while, there is NO way DFW did NOT know that.  Therefore, for me his ramblings sound insincere in addition to their not fully formed thoughts.",
      "textOriginal": "For being such an intelligent, well-educated guy, David Foster Wallace, seems ignorant of the fact that our society is governed at the top by Luciferians which have directed every avenue our country is going.  And having attended elitist  Harvard for a while, there is NO way DFW did NOT know that.  Therefore, for me his ramblings sound insincere in addition to their not fully formed thoughts.",
      "canRate": true,
      "viewerRating": "none",
      "likeCount": 0,
      "publishedAt": "2017-08-11T15:56:11.000Z",
      "updatedAt": "2017-08-11T15:56:11.000Z"
     }
    },
    "canReply": true,
    "totalReplyCount": 0,
    "isPublic": true
   }
  },
  {
   "kind": "youtube#commentThread",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/cZwHkHckMJk49XnuMvJM6eUPtJ0\"",
   "id": "z225ituycwjds5acmacdp430w3y10i5qqqs41yira0lw03c010c",
   "snippet": {
    "videoId": "UKdZU9Db6fk",
    "topLevelComment": {
     "kind": "youtube#comment",
     "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/-ohev3Rr6lX39a0nMB40DITaVZU\"",
     "id": "z225ituycwjds5acmacdp430w3y10i5qqqs41yira0lw03c010c",
     "snippet": {
      "authorDisplayName": "Danny B.",
      "authorProfileImageUrl": "https://yt3.ggpht.com/-T3swRCxyb-A/AAAAAAAAAAI/AAAAAAAAAAA/eCwjmG62a5g/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
      "authorChannelUrl": "http://www.youtube.com/channel/UCLi0lEuBajks4aSAC_PGQJg",
      "authorChannelId": {
       "value": "UCLi0lEuBajks4aSAC_PGQJg"
      },
      "videoId": "UKdZU9Db6fk",
      "textDisplay": "With the whole gratification of needs bit, isn't he basically describing commodity fetishism. Because we feel empty inside, we are perpetual consumerists who believe (subconsciously) that the products have a special value (pleasure producing, status procuring qualities, etc.) that will fill the hole inside. But the reality is that it never does, yet we persist in the activity and it becomes a vicious cycle.\u003cbr /\u003e\u003cbr /\u003eIn the absence of a more spiritually invigorating set of cultural values, we are all without an anchor and lost. As DFW points out, we all have a religious impulse but worshipping stuff is ultimately vapid and unfulfilling. It's just another drug that we use to escape from facing ourselves because we are afraid of what we'll see.",
      "textOriginal": "With the whole gratification of needs bit, isn't he basically describing commodity fetishism. Because we feel empty inside, we are perpetual consumerists who believe (subconsciously) that the products have a special value (pleasure producing, status procuring qualities, etc.) that will fill the hole inside. But the reality is that it never does, yet we persist in the activity and it becomes a vicious cycle.\n\nIn the absence of a more spiritually invigorating set of cultural values, we are all without an anchor and lost. As DFW points out, we all have a religious impulse but worshipping stuff is ultimately vapid and unfulfilling. It's just another drug that we use to escape from facing ourselves because we are afraid of what we'll see.",
      "canRate": true,
      "viewerRating": "none",
      "likeCount": 0,
      "moderationStatus": "likelySpam",
      "publishedAt": "2017-08-07T14:55:23.000Z",
      "updatedAt": "2017-08-07T15:01:25.000Z"
     }
    },
    "canReply": true,
    "totalReplyCount": 0,
    "isPublic": true
   }
  }
  // Many more comments
 ]
}

1 个答案:

答案 0 :(得分:0)

谢谢@CBroe

这部分代码工作正常

foreach ($ytdata->items as $item) {
    echo "Author: ". $item->snippet->topLevelComment->snippet->authorDisplayName;
    echo "Comment: ". $item->snippet->topLevelComment->snippet->textDisplay;
}

但是当我在foreach中添加foreach时,我得到的是错误结果