json — YouTube API отображает & quot; ответы & quot;

Как я могу отобразить ответы в комментариях 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, но результат

«Попытка получить свойство необъекта» «Указан неверный аргумент для 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
]
}

0

Решение

Спасибо @CBroe

эта часть кода работает нормально

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

но когда я добавляю foreach внутрь foreach, это время, когда я получаю результат ошибки

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector