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);
}
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.
I have a PHP file which gets a JSON object called $response. The $response has many values (as shown below) and I want to extract a few includes title and price. I have tried the below code but it doesnt show ANY result (no error, no value!). Thoughts?
Thank you
PHP code:
$data = json_decode($response, true);
echo $data->ItemsResult->Items[0]->ItemInfo->Title->DisplayValue;
$response =
{
"ItemsResult": {
"Items": [{
"ASIN": "B0825SNHP1",
"BrowseNodeInfo": {
"BrowseNodes": [{
"Ancestor": {
"Ancestor": {
"Ancestor": {
"ContextFreeName": "Toys & Games",
"DisplayName": "Toys & Games",
"Id": "165793011"
},
"ContextFreeName": "Toys & Games",
"DisplayName": "Categories",
"Id": "165795011"
},
"ContextFreeName": "Stuffed Animals & Plush Toys",
"DisplayName": "Stuffed Animals & Plush Toys",
"Id": "166461011"
},
"ContextFreeName": "Plush Figure Toys",
"DisplayName": "Plush Figures",
"Id": "11350121011",
"IsRoot": false,
"SalesRank": 1
}],
"WebsiteSalesRank": {
"ContextFreeName": "Toys & Games",
"DisplayName": "Toys & Games",
"SalesRank": 32
}
},
"DetailPageURL": "https://www.amazon.com/dp/B0825SNHP1?tag=tpf0bee-20&linkCode=ogi&th=1&psc=1",
"Images": {
"Primary": {
"Large": {
"Height": 500,
"URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L.jpg",
"Width": 467
},
"Medium": {
"Height": 160,
"URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L._SL160_.jpg",
"Width": 149
},
"Small": {
"Height": 75,
"URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L._SL75_.jpg",
"Width": 70
}
},
"Variants": [{
"Large": {
"Height": 450,
"URL": "https://m.media-amazon.com/images/I/51myKYEuuuL.jpg",
"Width": 500
},
"Medium": {
"Height": 144,
"URL": "https://m.media-amazon.com/images/I/51myKYEuuuL._SL160_.jpg",
"Width": 160
},
"Small": {
"Height": 68,
"URL": "https://m.media-amazon.com/images/I/51myKYEuuuL._SL75_.jpg",
"Width": 75
}
}, {
"Large": {
"Height": 500,
"URL": "https://m.media-amazon.com/images/I/515j0HmJV0L.jpg",
"Width": 500
},
"Medium": {
"Height": 160,
"URL": "https://m.media-amazon.com/images/I/515j0HmJV0L._SL160_.jpg",
"Width": 160
},
"Small": {
"Height": 75,
"URL": "https://m.media-amazon.com/images/I/515j0HmJV0L._SL75_.jpg",
"Width": 75
}
}, {
"Large": {
"Height": 466,
"URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L.jpg",
"Width": 500
},
"Medium": {
"Height": 149,
"URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L._SL160_.jpg",
"Width": 160
},
"Small": {
"Height": 70,
"URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L._SL75_.jpg",
"Width": 75
}
}, {
"Large": {
"Height": 500,
"URL": "https://m.media-amazon.com/images/I/414gFkO4AmL.jpg",
"Width": 446
},
"Medium": {
"Height": 160,
"URL": "https://m.media-amazon.com/images/I/414gFkO4AmL._SL160_.jpg",
"Width": 143
},
"Small": {
"Height": 75,
"URL": "https://m.media-amazon.com/images/I/414gFkO4AmL._SL75_.jpg",
"Width": 67
}
}, {
"Large": {
"Height": 500,
"URL": "https://m.media-amazon.com/images/I/516zb5W2o3L.jpg",
"Width": 476
},
"Medium": {
"Height": 160,
"URL": "https://m.media-amazon.com/images/I/516zb5W2o3L._SL160_.jpg",
"Width": 152
},
"Small": {
"Height": 75,
"URL": "https://m.media-amazon.com/images/I/516zb5W2o3L._SL75_.jpg",
"Width": 71
}
}]
},
"ItemInfo": {
"ByLineInfo": {
"Brand": {
"DisplayValue": "Mattel",
"Label": "Brand",
"Locale": "en_US"
},
"Manufacturer": {
"DisplayValue": "Mattel",
"Label": "Manufacturer",
"Locale": "en_US"
}
},
"Classifications": {
"Binding": {
"DisplayValue": "Accessory",
"Label": "Binding",
"Locale": "en_US"
},
"ProductGroup": {
"DisplayValue": "Toy",
"Label": "ProductGroup",
"Locale": "en_US"
}
},
"ContentInfo": {
"Edition": {
"DisplayValue": "Star Wars Edition",
"Label": "Edition",
"Locale": "en_US"
}
},
"ExternalIds": {
"EANs": {
"DisplayValues": ["0887961938814"],
"Label": "EAN",
"Locale": "en_US"
},
"UPCs": {
"DisplayValues": ["887961938814"],
"Label": "UPC",
"Locale": "en_US"
}
},
"Features": {
"DisplayValues": ["This 11-inch The Child plush toy will capture the hearts of Star Wars fans everywhere", "Inspired by the Disney+ series The Mandalorian, the adorable figure with green skin, big ears and large eyes resembles a baby Yoda but is referred to as The Child.", "The toy plush has a soft body, plus a sturdy base filled with beans, perfect for cuddling or display as a collectible", "The character wears his robes as seen in the show.", "Star Wars fans will love taking on the role of The Mandalorian Bounty Hunter and caring for The Child on their own", "Material Type: Polyester"],
"Label": "Features",
"Locale": "en_US"
},
"ManufactureInfo": {
"ItemPartNumber": {
"DisplayValue": "GWD85",
"Label": "PartNumber",
"Locale": "en_US"
},
"Model": {
"DisplayValue": "GWD85",
"Label": "Model",
"Locale": "en_US"
},
"Warranty": {
"DisplayValue": "No Warranty",
"Label": "Warranty",
"Locale": "en_US"
}
},
"ProductInfo": {
"Color": {
"DisplayValue": "Green",
"Label": "Color",
"Locale": "en_US"
},
"IsAdultProduct": {
"DisplayValue": false,
"Label": "IsAdultProduct",
"Locale": "en_US"
},
"ItemDimensions": {
"Height": {
"DisplayValue": 11.02,
"Label": "Height",
"Locale": "en_US",
"Unit": "Inches"
},
"Length": {
"DisplayValue": 5.98,
"Label": "Length",
"Locale": "en_US",
"Unit": "Inches"
},
"Weight": {
"DisplayValue": 0.220462262,
"Label": "Weight",
"Locale": "en_US",
"Unit": "Pounds"
},
"Width": {
"DisplayValue": 7.99,
"Label": "Width",
"Locale": "en_US",
"Unit": "Inches"
}
},
"ReleaseDate": {
"DisplayValue": "2020-02-12T00:00:01Z",
"Label": "ReleaseDate",
"Locale": "en_US"
},
"Size": {
"DisplayValue": "11 inches",
"Label": "Size",
"Locale": "en_US"
},
"UnitCount": {
"DisplayValue": 1,
"Label": "NumberOfItems",
"Locale": "en_US"
}
},
"Title": {
"DisplayValue": "Mattel Star Wars The Child Plush Toy, 11-Inch Small Yoda-Like Soft Figure from The Mandalorian, Green",
"Label": "Title",
"Locale": "en_US"
}
},
"Offers": {
"Listings": [{
"Availability": {
"Message": "In stock. Usually ships within 4 to 5 days.",
"MinOrderQuantity": 1,
"Type": "Now"
},
"Condition": {
"SubCondition": {
"Value": "New"
},
"Value": "New"
},
"DeliveryInfo": {
"IsAmazonFulfilled": false,
"IsFreeShippingEligible": false,
"IsPrimeEligible": false
},
"Id": "xZd%2BKU9rGH7fWwKPEUHMfhsQHzl3QpAN6kIllBjmOl90egwIXEDSkqIt1nqy2q90nMMKYhKCECzkZugn%2FhS6MNMQ0DeGGHgqoDimWML40ChnAKQi3WGnvzASkBlZn3fOYl%2Fk7qoY%2FkafbujzE4UkOjHO6D2nEkcs",
"IsBuyBoxWinner": false,
"MerchantInfo": {
"DefaultShippingCountry": "US",
"FeedbackCount": 261,
"FeedbackRating": 3.53,
"Id": "A111I7FGCUO8HR",
"Name": "ZDeals"
},
"Price": {
"Amount": 27.58,
"Currency": "USD",
"DisplayAmount": "$27.58"
},
"ProgramEligibility": {
"IsPrimeExclusive": false,
"IsPrimePantry": false
},
"ViolatesMAP": false
}],
"Summaries": [{
"Condition": {
"Value": "Collectible"
},
"HighestPrice": {
"Amount": 68.94,
"Currency": "USD",
"DisplayAmount": "$68.94"
},
"LowestPrice": {
"Amount": 58.99,
"Currency": "USD",
"DisplayAmount": "$58.99"
},
"OfferCount": 2
}, {
"Condition": {
"Value": "New"
},
"HighestPrice": {
"Amount": 58.75,
"Currency": "USD",
"DisplayAmount": "$58.75"
},
"LowestPrice": {
"Amount": 22.48,
"Currency": "USD",
"DisplayAmount": "$22.48"
},
"OfferCount": 224
}]
}
}]
}
}
The second argument of json_decode function enables associative array result. So in your case you can either do
$data = json_decode($response);
echo $data->ItemsResult->Items[0]->ItemInfo->Title->DisplayValue;
or
$data = json_decode($response, true);
echo $data['ItemsResult']['Items'][0]['ItemInfo']['Title']['DisplayValue'];
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.
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"];