This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I've been trying to access the "videoID" JSON object in the following array:
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/652q8gXfNeBDSoSQrv8VCrAv0Ho\"",
"nextPageToken": "CAUQAA",
"regionCode": "AU",
"pageInfo": {
"totalResults": 25,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/31YuvB6enDzigibEfdgUq4-ZYc0\"",
"id": {
"kind": "youtube#video",
"videoId": "Ldx1nwBd4AY"
},
"snippet": {
"publishedAt": "2017-04-14T21:00:00.000Z",
"channelId": "UC-z95jtL6-oDyFueQPoZLfQ",
"title": "HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS Tutorial - Tech Scout",
"description": "LIKE THE VIDEO + TURN ON CHANNEL NOTIFICATIONS! HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Tech Scout",
"liveBroadcastContent": "none"
}
},
It continues from there but I've been trying to access the "videoID" object using the following code:
$youtubeurl = "https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" . $channelid . "&key=" . $api;
$youtubeinfo = file_get_contents($youtubeurl);
$youtubeencoded = json_decode($youtubeinfo, true);
$youtubevideoid = $youtubeencoded["data"]["items"][0]["videoID"];
echo $youtubevideoid;
I don't have too much experience with JSON so I just got the code in the 4th line (which I think is the problem) from another question on Stack Overflow.
You don't have node data in your JSON. You also missed node id. Also, it's videoId and not videoID:
$youtubevideoid = $youtubeencoded["items"][0]["id"]["videoId"];
Try
$youtubevideoid = $youtubeencoded["items"][0]["id"]["videoId"];
p.s. updated
Your json file is wrong.it will be like that
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/652q8gXfNeBDSoSQrv8VCrAv0Ho\"",
"nextPageToken": "CAUQAA",
"regionCode": "AU",
"pageInfo": {
"totalResults": 25,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/31YuvB6enDzigibEfdgUq4-ZYc0\"",
"id": {
"kind": "youtube#video",
"videoId": "Ldx1nwBd4AY"
},
"snippet": {
"publishedAt": "2017-04-14T21:00:00.000Z",
"channelId": "UC-z95jtL6-oDyFueQPoZLfQ",
"title": "HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS Tutorial - Tech Scout",
"description": "LIKE THE VIDEO + TURN ON CHANNEL NOTIFICATIONS! HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS ...",
"thumbnails": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Tech Scout",
"liveBroadcastContent": "none"
}
]
}
And then you can use this code
$data = json_decode($youtubejson,true);
echo $data["kind"];
Related
I am using this API call to get the Page's Post details and this one includes the Post's Video Source.
https://graph.facebook.com/v5.0/{page id}/posts?fields=from{id,name,picture},status_type,attachments,full_picture,picture,message,story,properties,created_time,shares,comments.limit(0).summary(true).as(comments),likes.limit(0).summary(true).as(likes),permalink_url
This API call responds with these data:
{
"from": {
"id": "123123",
"name": "abcd",
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "https://scontent.fmnl4-6.fna.fbcdn.net/v/t1.0-1/cp0/p50x50/123123_123123",
"width": 50
}
}
},
"status_type": "added_video",
"attachments": {
"data": [
{
"media": {
"image": {
"height": 405,
"src": "https://fb.com/adsfasdf",
"width": 720
},
"source": "https://video-fb-source.com.fb.mp4?_nc_cat=1231&cb=3"
},
"target": {
"id": "240206797735633",
"url": "https://www.facebook.com/asd1asdfasdf"
},
"title": "test 123123",
"type": "video_autoplay",
"url": "https://www.facebook.com/hahahae/videos/240206797735633/"
}
]
},
"full_picture": "https://scontent.fmnl4-6.fna.fbcdn.net/v/t15.5256-10/145288627_240218977734415_42016794327.jpg?",
"picture": "https://scontent.fmnl4-6.fna.fbcdn.net/v/t15.5256-10/145288627_240218977734415_42016794327.jpg?",
"message": "haha hahahaha",
"properties": [
{
"name": "Length",
"text": "41:20"
}
],
"created_time": "2021-02-15T18:00:01+0000",
"shares": {
"count": 7
},
"comments": {
"data": [
],
"summary": {
"order": "ranked",
"total_count": 1,
"can_comment": true
}
},
"likes": {
"data": [
],
"summary": {
"total_count": 16,
"can_like": true,
"has_liked": false
}
},
"permalink_url": "https://www.facebook.com/xxxx/posts/xxx/",
"id": "xxxx_123123_xxx"
},
I get the Post's Video Source from the attachments.data[0].media.source json, then every time I get the video, I download it on my local.
My problem with that is it does not return a high-quality video, it's a bit pixelated. Unlike in Facebook, when you go directly to the video URL, there's a 1080p option.
How can I get a high-quality video that I can download through Facebook's Graph API?
Thanks.
How to get first videoId , title , description and thumbnails high value from this json format ?
I use this this code
<?PHP
$curl_value = curl_init('https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=3&search_query=test&key=youtube_api_key');
curl_setopt($curl_value, CURLOPT_RETURNTRANSFER, true);
$json_exec = curl_exec($curl_value);
$result_json = json_decode($json_exec);
$video_id = $result_json->items[0]->videoId;
$title = $result_json->items[0]->snippet->title;
$description = $result_json->items[0]->snippet->description;
$thumbnails = $result_json->items[0]->snippet->thumbnails->high;
echo $video_id;
echo $title;
echo $description;
echo $thumbnails;
?>
.
But not get any data , how can i do ?
For this case i want to get
videoId = iKzRIweSBLA
title = Ed Sheeran - Perfect [Official Lyric Video]
description = Out Now: https://atlanti.cr/yt-album Subscribe to Ed's channel: http://test.ly/SubscribeToEdSheeran Follow Ed on... Facebook: ...
thumbnails = https://i.ytimg.com/vi/iKzRIweSBLA/hqdefault.jpg
This is json result
{
"kind": "youtube#searchListResponse",
"etag": "\"cbz3lIQ2N25AfwNr-BdxUVxJ_QY/fkLih7vu8hjlSWPap-46qmMBkKw\"",
"nextPageToken": "CAMQAA",
"regionCode": "TH",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 3
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"cbz3lIQ2N25AfwNr-BdxUVxJ_QY/xzdNtvTMaihnGappsfbZRCdysc4\"",
"id": {
"kind": "youtube#video",
"videoId": "iKzRIweSBLA"
},
"snippet": {
"publishedAt": "2017-09-22T07:02:24.000Z",
"channelId": "UC0C-w0YjGpqDXGB8IHb662A",
"title": "Ed Sheeran - Perfect [Official Lyric Video]",
"description": "Out Now: https://atlanti.cr/yt-album Subscribe to Ed's channel: http://test.ly/SubscribeToEdSheeran Follow Ed on... Facebook: ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/iKzRIweSBLA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/iKzRIweSBLA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/iKzRIweSBLA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Ed Sheeran",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"cbz3lIQ2N25AfwNr-BdxUVxJ_QY/7lxJIhHg_j7RWlEuZp87hFExzCg\"",
"id": {
"kind": "youtube#video",
"videoId": "nfs8NYg7yQM"
},
"snippet": {
"publishedAt": "2017-04-24T17:00:05.000Z",
"channelId": "UCwppdrjsBPAZg5_cUwQjfMQ",
"title": "Charlie Puth - Attention [Official Video]",
"description": "Download and Stream \"Attention\": https://Atlantic.lnk.to/AttentionID Pre-Order Voicenotes: https://Atlantic.lnk.to/VoicenotesID Exclusive VoiceNotes Merchandise ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/nfs8NYg7yQM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/nfs8NYg7yQM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/nfs8NYg7yQM/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Charlie Puth",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"cbz3lIQ2N25AfwNr-BdxUVxJ_QY/HOZpkTZCsq-gixbvF4mQGcdiepE\"",
"id": {
"kind": "youtube#video",
"videoId": "kXYiU_JCYtU"
},
"snippet": {
"publishedAt": "2007-03-05T08:12:00.000Z",
"channelId": "UCZU9T1ceaOgwfLRq7OKFU4Q",
"title": "Numb (Official Video) - Linkin Park",
"description": "Linkin Park \"Numb\" off of the album METEORA. Directed by Joe Hahn. http://www.linkinpark.com | http://LPUnderground.com iTunes: http://go.lprk.co/ml/3pb/ ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/kXYiU_JCYtU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/kXYiU_JCYtU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/kXYiU_JCYtU/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Linkin Park",
"liveBroadcastContent": "none"
}
}
]
}
Your title and description already work; video_id and thumbnails are very close.
$video_id = $result_json->items[0]->id->videoId;
$title = $result_json->items[0]->snippet->title;
$description = $result_json->items[0]->snippet->description;
$thumbnails = $result_json->items[0]->snippet->thumbnails->high->url;
Let's say I do a simple youtube search using the YouTube API v3, and I get a response like this:
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/1MwZbx-vX21eNjUJjkUlNHKCIhI\"",
"nextPageToken": "CAIQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 412,
"resultsPerPage": 2
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/q2KHbIuwnAhM64HgrqhBqc5xvTI\"",
"id": {
"kind": "youtube#video",
"videoId": "gpZvuZEiINA"
},
"snippet": {
"publishedAt": "2014-03-30T11:46:50.000Z",
"channelId": "UCCaE0Bj6NI-y8_yL1FpcJUw",
"title": "Depression-90` instrumental (download link)",
"description": "E-Mail: ensari5500#gmail.com\nNew old school instrumental with download link ,Enjoy the beat, I hope you like it!\nPlease comment, rate and subscribe if you like this beat\nI appreciate YOUR support! Peace and respect",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/gpZvuZEiINA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/gpZvuZEiINA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/gpZvuZEiINA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Ekii020 90` OLDSCHOOL - BOOMBOOMBAP INSTRUMENTALS",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HXRyGhFaD4Cn0wvF5XPMJKX5tNQ\"",
"id": {
"kind": "youtube#video",
"videoId": "G7ThqpcuPTI"
},
"snippet": {
"publishedAt": "2017-08-05T23:30:24.000Z",
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"title": "Homeless (Dark Sad Piano Hip Hop Rap Instrumental Beat)",
"description": "http://rightbeatradio.com/product/homeless/\n\nI wrote a song about a humble man I met yesterday. We spoke for a while. He just wanted to talk. He was homeless.\n\nI cut some pieces from a few jazz recordings and put them behind a simple piano riff.\n\nThis music has a lot of feeling. Perfectly describes how I felt after our conversation.\n\nTake care.\n\n89 bpm\n\nrightbeatradio.com\ntwitter.com/rightbeatradio",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/G7ThqpcuPTI/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/G7ThqpcuPTI/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/G7ThqpcuPTI/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Right Beat Radio",
"liveBroadcastContent": "none"
}
}
]
}
I would like to generate, say, 10 results, but then only pick 1 (one) random [item] from the response. How can I randomize the results, and only pick one of them from this entire response?
I've seen functions such as shuffle() and array_rand() but these don't seem to be the functions that I need.
I have also seen a snippet that goes like this:
$array = json_decode($JSON, true);
$random_entry = array_rand($array['items'], 1);
$json_data = json_encode($random_entry);
I've tried that, but it returns only strings like "nextPageToken" or "kind", etc.
I would like to pick one entire [item] and grab all of its relevant [snippet] data, such as channelId, title, description, thumbnails, etc.
Can anyone point me in the right direction?
Try this
$array = json_decode($JSON, true);
$items = $array['items'];
$random_key = array_rand($items, 1);
$json_data = json_encode($items[$random_key]);
array_rand() return array key, not array value.
When using the youtube data ip I cannot retrieve a video id. I can use all other fields but get an error accessing the videoId field. The following:
https://www.googleapis.com/youtube/v3/search?key={api_key}&channelId=UCXuqSBlHAE6Xw-yeJA0Tunw&part=snippet,id&order=date&maxResults=2
returns
{
"kind": "youtube#searchListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/ycDRkf40ZrOgEHl0cz-4b60EIFc\"",
"nextPageToken": "CAIQAA",
"regionCode": "GB",
"pageInfo": {
"totalResults": 3510,
"resultsPerPage": 2
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/HOEmnNQHjX9o0bjBMiW0ipFEyl0\"",
"id": {
"kind": "youtube#video",
"videoId": "dkDq3JJ6IvM"
},
"snippet": {
"publishedAt": "2016-10-04T07:05:26.000Z",
"channelId": "UCXuqSBlHAE6Xw-yeJA0Tunw",
"title": "Best All-around Gaming Laptop? - MSI GS63VR Review",
"description": "The GS63VR from MSI is our first GTX 1060-equipped gaming laptop. And man, are we impressed... TunnelBear message: TunnelBear is the easy-to-use VPN ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "LinusTechTips",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/qXo91IDtZrS-0G5AOWB--t3pR4Y\"",
"id": {
"kind": "youtube#video",
"videoId": "g7UX0nP70E0"
},
"snippet": {
"publishedAt": "2016-10-03T06:50:31.000Z",
"channelId": "UCXuqSBlHAE6Xw-yeJA0Tunw",
"title": "The best retro gaming experience ever?",
"description": "Controllers are truly a dime a dozen nowadays... But do these retro-inspired controllers, and their wireless adapters, stand out from the pack? iFixit link: Use offer ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/g7UX0nP70E0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/g7UX0nP70E0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/g7UX0nP70E0/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "LinusTechTips",
"liveBroadcastContent": "none"
}
}
]
}
Now if I
$videos = json_decode($result)
foreach($videos->items as $video)
{
print_r($video->id->videoId);
}
I get an error
[ErrorException]
Undefined property: stdClass::$videoId
however
print_r($video->id->kind) returns the desired result
also print_r($video->id) returns:
stdClass Object
(
[kind] => youtube#video
[videoId] => dkDq3JJ6IvM
)
It turns out my comment above wasn't the solution.
In actual fact the foreach loop was the issue for some reason:
foreach($videos->items as $video)
{
print_r($video->id->videoId);
}
This didn't work and resulted in the error. Simply replacing $video in the foreach loop with a different word (in this case item) solved it:
foreach($videos->items as $item)
{
print_r($item->id->videoId);
}
I have used the Facebook graph link in my site, I have tried for the last two days but I am not able to fix this problem. For two months this code has been working fine but right now it is not working.
Here is the Link
https://graph.facebook.com/157705150944797/photos/uploaded
Why do I get this error?
{
"error": {
"message": "Unsupported get request.",
"type": "GraphMethodException",
"code": 100
}
}
But when I use the following link I will get a response
https://graph.facebook.com/157705150944797/photos/
{
"data": [
{
"id": "212708942111084",
"from": {
"category": "Local business",
"category_list": [
{
"id": "206284062724685",
"name": "Roofer"
}
],
"name": "Blue Sky Roofing, Inc.",
"id": "157705150944797"
},
"picture": "https://m.ak.fbcdn.net/sphotos-e.ak/hphotos-ak-xap1/v/t1.0-9/p130x130/284949_212708942111084_583574_n.jpg?oh=3554b061f512bb97cb577049578d5532&oe=54D26E47&__gda__=1427888341_67dd755838624aa1e322faa03d8e4325",
"source": "https://m.ak.fbcdn.net/sphotos-e.ak/hphotos-ak-xap1/v/t1.0-9/284949_212708942111084_583574_n.jpg?oh=8e26df9a434160f154dc0a4ad6690fad&oe=54D99C4B&__gda__=1424573780_940c191fc86f99e5316f39a70336606c",
"height": 445,
"width": 445,
"images": [
{
"height": 445,
"width": 445,
"source": "https://m.ak.fbcdn.net/sphotos-e.ak/hphotos-ak-xap1/v/t1.0-9/284949_212708942111084_583574_n.jpg?oh=8e26df9a434160f154dc0a4ad6690fad&oe=54D99C4B&__gda__=1424573780_940c191fc86f99e5316f39a70336606c"
},
...
I don't know why I get that error when I use this link. I want to get all the uploaded photos in the site.
https://graph.facebook.com/157705150944797/photos/uploaded