I have a JSON from telegram, In php How can find that if it is a text do something, but if its photo, audio or ... do something else. this part is located after date, how can I access it?
I tried by access it by index like ['message'][1] but I got error.
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
},
You need to use json_decode function.
I use foreach loop then echo $item['message']['message_id'] to display message_id in following example.
Try Example
$json = <<<JSON
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
}
]
}
JSON;
$json_data = json_decode($json, true);
foreach($json_data['result'] as $item)
{
if(isset($item['message']['text']))
{
echo $item['message']['text']."<br />";
}
if(isset($item['message']['photo']))
{
foreach($item['message']['photo'] as $photo)
{
echo "file_id: ".$photo['file_id']."<br />";
echo "file_size: ".$photo['file_size']."<br />";
}
}
}
Related
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'];
I am using below php code to use json in my file but i am not getting the ouput . I need to print image url from Json array . Can any one help.
Json data is :
{
"pagination": {},
"data": [
{
"id": "1980025670135540608_1039191866",
"user": {},
"images": {
"thumbnail": {
"width": 150,
"height": 150,
"url": "https://scontent.cdninstagram.com/vp/d3d00967ec82131dedfdddb0cb8f8bc7/5CFCE4F1/t51.2885-15/e35/s150x150/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"low_resolution": {
"width": 320,
"height": 320,
"url": "https://scontent.cdninstagram.com/vp/5cb939b696c2d5a0211572bd0e965bee/5CF41C01/t51.2885-15/e35/s320x320/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"standard_resolution": {
"width": 640,
"height": 640,
"url": "https://scontent.cdninstagram.com/vp/411df4ade429230dee0ab3631d25a6eb/5CE56DB3/t51.2885-15/e35/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
}
},
"created_time": "1550257475",
"caption": null,
"user_has_liked": false,
"likes": {
"count": 0
},
"tags": {},
"filter": "Normal",
"comments": {
"count": 0
},
"type": "image",
"link": "https://www.instagram.com/p/Bt6dts6lWuA/",
"location": null,
"attribution": null,
"users_in_photo": {}
},
{
"id": "1979798389190546391_1039191866",
"user": {
"id": "1039191866",
"full_name": "Jagjeet Kumar",
"profile_picture": "https://scontent.cdninstagram.com/vp/9871a297c832e1b1ef249028856af412/5CFC0D7D/t51.2885-19/11931260_1465802173750145_760962573_a.jpg?_nc_ht=scontent.cdninstagram.com",
"username": "jagjeet_k"
},
"images": {
"thumbnail": {
"width": 150,
"height": 150,
"url": "https://scontent.cdninstagram.com/vp/5f7e27fbf6d878bb85be23b70b637866/5CF8244F/t51.2885-15/e35/s150x150/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"low_resolution": {
"width": 320,
"height": 320,
"url": "https://scontent.cdninstagram.com/vp/16376fe1eb033705d1c7b1f394bef013/5CE38537/t51.2885-15/e35/s320x320/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"standard_resolution": {
"width": 640,
"height": 640,
"url": "https://scontent.cdninstagram.com/vp/c51878930ed8c98a595f16299f5c18d7/5CF9E7CA/t51.2885-15/sh0.08/e35/s640x640/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
}
},
"created_time": "1550230381",
"caption": null,
"user_has_liked": false,
"likes": {
"count": 1
},
"tags": {},
"filter": "Crema",
"comments": {
"count": 1
},
"type": "image",
"link": "https://www.instagram.com/p/Bt5qCVCl9PX/",
"location": null,
"attribution": null,
"users_in_photo": {}
}
],
"meta": {
"code": 200
}
}
Php code which I am using :
<?php
$instagram_feed_data = json_decode($mediacount, true);
foreach ($instagram_feed_data->data as $item) {
$img_url = $item['images']['low_resolution']['url'];
?>
<img src="<?= $img_url; ?>">
<?php
}
I have to print the image url which is in low resolution array .
$instagram_feed_data = json_decode($mediacount, true);
Passing a true value to the second parameter of json_decode() tells PHP that you want an array back instead of an object. So you can't dereference it with the object handle like this:
foreach ($instagram_feed_data->data as $item) {
You want an array reference like this:
foreach ($instagram_feed_data['data'] as $item) {
I'm trying to display a JSON Array in php
JSON:
"members":[
{
"username":"Abstract",
"status":"online",
"bot":true,
"nick":"ViralBot \ud83d\ude0b",
"avatar_url":"https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar":"59b492151d2c352f3ac5e249e4f04a82",
"discriminator":"0612",
"id":"133718676741292033"
},
{
"username":"Aethex",
"status":"online",
"bot":true,
"game":{
"name":"aethex.xyz | -help"
},
"avatar_url":"https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar":"bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator":"0394",
"id":"150300454708838401"
},
{
"username":"AIRHORN SOLUTIONS",
"status":"online",
"bot":true,
"nick":"MLG AIRHORNZ!",
"game":{
"name":"airhornbot.com"
},
"avatar_url":"https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar":"96be9927ca947a75021db568485612fc",
"discriminator":"6723",
"id":"159800228088774656"
},
{
"username":"BuddyGang",
"status":"online",
"game":{
"name":"Custom Widgets API"
},
"avatar_url":"https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar":"089e84734ea5ca86d20292abcca0982f",
"discriminator":"1704",
"id":"97172171259904000"
},
{
"username":"danclay",
"status":"dnd",
"nick":"danclay \ud83d\ude09",
"avatar_url":"https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar":"fa460980203d7838c8481c93c0361f6d",
"discriminator":"2051",
"id":"97147476531757056"
},
{
"username":"dragongod100",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar":"5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator":"5291",
"id":"96716994136514560"
},
{
"username":"Hax0nWax0ff",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar":"ccf68e818fa44e342f60030a89a0fd53",
"discriminator":"7691",
"id":"125738028793593856"
},
{
"username":"iiPsionic",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar":"ba4bba83436f227933004694b4c184f2",
"discriminator":"6034",
"id":"142014347038818304"
},
{
"username":"RH1-N0",
"status":"online",
"bot":true,
"nick":"RHINO ATTACK!",
"avatar_url":"https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar":"fe0070998d6c9378edf8243e3267f950",
"discriminator":"6993",
"id":"135288293548883969"
},
{
"username":"WildBot",
"status":"online",
"bot":true,
"nick":"Cra-Cra Bot \ud83d\ude31",
"avatar_url":"https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar":"f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator":"3942",
"id":"110462073074388992"
}
]
I want to be able to display it in numeric order of the "position" variable, with the avatar positioned next to the username then the game text.
HTML layout:
<img src="AVATAR_URL" width="50%"> USERNAME - <small>GAME</small><br>
try this code bro, hope it helps
$arr=json_decode($data,true);
// to prevent error undefined index
if(isset($arr['members'])) {
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
}
Here the code:-
$data='{
"members": [
{
"username": "Abstract",
"status": "online",
"bot": true,
"nick": "ViralBot 😋",
"avatar_url": "https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar": "59b492151d2c352f3ac5e249e4f04a82",
"discriminator": "0612",
"id": "133718676741292033"
},
{
"username": "Aethex",
"status": "online",
"bot": true,
"game": {
"name": "aethex.xyz | -help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar": "bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator": "0394",
"id": "150300454708838401"
},
{
"username": "AIRHORN SOLUTIONS",
"status": "online",
"bot": true,
"nick": "MLG AIRHORNZ!",
"game": {
"name": "airhornbot.com"
},
"avatar_url": "https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar": "96be9927ca947a75021db568485612fc",
"discriminator": "6723",
"id": "159800228088774656"
},
{
"username": "BuddyGang",
"status": "online",
"game": {
"name": "Custom Widgets API"
},
"avatar_url": "https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar": "089e84734ea5ca86d20292abcca0982f",
"discriminator": "1704",
"id": "97172171259904000"
},
{
"username": "danclay",
"status": "dnd",
"nick": "danclay 😉",
"avatar_url": "https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar": "fa460980203d7838c8481c93c0361f6d",
"discriminator": "2051",
"id": "97147476531757056"
},
{
"username": "dragongod100",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar": "5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator": "5291",
"id": "96716994136514560"
},
{
"username": "Hax0nWax0ff",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar": "ccf68e818fa44e342f60030a89a0fd53",
"discriminator": "7691",
"id": "125738028793593856"
},
{
"username": "iiPsionic",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar": "ba4bba83436f227933004694b4c184f2",
"discriminator": "6034",
"id": "142014347038818304"
},
{
"username": "RH1-N0",
"status": "online",
"bot": true,
"nick": "RHINO ATTACK!",
"avatar_url": "https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar": "fe0070998d6c9378edf8243e3267f950",
"discriminator": "6993",
"id": "135288293548883969"
},
{
"username": "WildBot",
"status": "online",
"bot": true,
"nick": "Cra-Cra Bot 😱",
"avatar_url": "https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar": "f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator": "3942",
"id": "110462073074388992"
}
]
}';
$arr=json_decode($data,true);
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
Im working with Laravel 5.1 and have a small issue.
I am sending a JSON response from Instagram's API for most popular media from my controller and want to grab that response in my view with ajax and show it on page to the user without loading the entire page.
I am trying to grab the username and display it in my view to the user.
Right now nothing happens and the console does not show anything, it is empty.
This is my code:
CONROLLER:
public function PopularPics(City $city){
try{
$jsonData = $city->getMostPopularPics();
return response()->json($jsonData);
}catch(\Exception $ex){
return $ex->getCode();
}
}
VIEW
<ul id="theData">
</ul>
<script>
$(function(){
$("#JRequest").click(function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: "popular",
dataType:"json",
success:function(items){
$.each(items.data,function(i,item){
console.log(item.user.username);
});
}
});
});
});
</script>
INSTAGRAM JSON
{
"data": [{
"type": "image",
"users_in_photo": [],
"filter": "Gotham",
"tags": [],
"comments": { ... },
"caption": {
"created_time": "1296656006",
"text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)",
"from": {
"username": "cocomiin",
"full_name": "",
"type": "user",
"id": "1127272"
},
"id": "26329105"
},
"likes": {
"count": 35,
"data": [{
"username": "mikeyk",
"full_name": "Kevin S",
"id": "4",
"profile_picture": "..."
}, {...subset of likers...}]
},
"link": "http://instagr.am/p/BV5v_/",
"user": {
"username": "cocomiin",
"full_name": "Cocomiin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg",
"id": "1127272"
},
"created_time": "1296655883",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",
"width": 612,
"height": 612
}
},
"id": "22518783",
"location": null
},
{
"type": "video",
"videos": {
"low_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
"width": 640,
"height": 640
},
"users_in_photo": null,
"filter": "Vesper",
"tags": [],
"comments": {
"data": [{
"created_time": "1279332030",
"text": "Love the sign here",
"from": {
"username": "mikeyk",
"full_name": "Mikey Krieger",
"id": "4",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
},
"id": "8"
},
{
"created_time": "1279341004",
"text": "Chilako taco",
"from": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"id": "3"
}],
"count": 2
},
"caption": null,
"likes": {
"count": 1,
"data": [{
"username": "mikeyk",
"full_name": "Mikeyk",
"id": "4",
"profile_picture": "..."
}]
},
"link": "http://instagr.am/p/D/",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "...",
"id": "3"
},
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
"width": 612,
"height": 612
}
},
"id": "3",
"location": null
},
...]
}
I am trying to access the urls of the first images that show up for tumblr searches using the tag search api. Below is the json for the test search I'm using.
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": [
{
"blog_name": "m00tzman",
"id": 37918165271,
"post_url": "http://m00tzman.tumblr.com/post/37918165271/test-image-for-tumblr-a…",
"slug": "test-image-for-tumblr-api-thingy",
"type": "photo",
"date": "2012-12-14 18:13:25 GMT",
"timestamp": 1355508805,
"state": "published",
"format": "html",
"reblog_key": "IwlKk9r4",
"tags": [
"g6845e"
],
"highlighted": [ ],
"note_count": 29,
"caption": "<p>test image for tumblr api thingy</p>",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 500,
"height": 628,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 400,
"height": 502,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 250,
"height": 314,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 100,
"height": 126,
"url": "http://24.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 75,
"height": 75,
"url": "http://24.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
}
],
"original_size": {
"width": 500,
"height": 628,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
}
}
]
}
]
}
My problem is that I am not able to echo out the url for the "original size" image. Here is my code so far:
<?php
$search = file_get_contents('http://api.tumblr.com/v2/tagged?tag=g6845e&api_key=(api key)');
$tumblrdata = json_decode($search, true);
$photos = $tumblrdata['photos']['original_size'];
$url = $photos['url'];
echo $url;
?>
Any guesses on what is going wrong here?
Try
$photos = $tumblrdata['response']['photos'][0]['original_size'];