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'];
Related
I'm trying to import something from an .xml file that I have no control over. Everything went fine until the content:encoded part.
I'm getting the xml like this to strip it of the CDATA Tags:
$xml_object = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
The looks like this, I'm trying to get rid of all the tags and just get the "contents.body" parts, but when I try to use json_decode($itemJson); it returns null. What should I do?
[{
"id": 34543,
"type": "0",
"order": 0,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 345654,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://link.com/1567819/98/eipej334.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 2131656668,
"type": "0",
"order": 1,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 1650030480,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://links.com/jf/819/156722819/34/343.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 1740427509,
"type": "0",
"order": 2,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 802708308,
"order": 0,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p>Lorem ipsum dolor sit amet.</p>"
}, {
"type": "html",
"value": "false"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818
}]
}, {
"id": 79506641,
"type": "0",
"order": 3,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 28476702,
"order": 0,
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p><span style="\
"font-weight:"
bold;\
"="
"><br></span></p><span style="\
"font-weight:"
bold;\
"="
">lorem upisn? </span>"
}, {
"type": "html",
"value": "false"
}]
}]
}]
}]
I'm struggling with accessing elements of arrays. I'm sure it's easy but I always get undefined index errors.
Display Array :
// Get response
$synresponse = curl_exec($chs);
// Decode
$variants = json_decode($synresponse, true);
Returns :
{
"code": 200,
"result": {
"sync_product": {
"id": 118425517,
"external_id": "5d02e1420b4007",
"name": "Women's Crop Top",
"variants": 4,
"synced": 4
},
"sync_variants": [
{
"id": 1335801688,
"external_id": "5d02e1420b4141",
"sync_product_id": 118425517,
"name": "Women's Crop Top - S",
"synced": true,
"variant_id": 8039,
"retail_price": "22.50",
"currency": "USD",
"product": {
"variant_id": 8039,
"product_id": 224,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8039_1550478004.jpg",
"name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / S)"
},
"files": [
{
"id": 120674338,
"type": "default",
"hash": "6238a66581153315488397a5788216b2",
"url": null,
"filename": "logo2.png",
"mime_type": "image/png",
"size": 163254,
"width": 1380,
"height": 1380,
"dpi": null,
"status": "ok",
"created": 1560465358,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
"visible": true
},
{
"id": 120686478,
"type": "preview",
"hash": "fa1a4b2f561f28529f4230ba49eca1db",
"url": null,
"filename": "mockup-79b7c377.png",
"mime_type": "image/png",
"size": 113878,
"width": 600,
"height": 600,
"dpi": 43,
"status": "ok",
"created": 1560469826,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
"visible": false
}
],
"options": []
},
{
"id": 1335801689,
"external_id": "5d02e1420b41b9",
"sync_product_id": 118425517,
"name": "Women's Crop Top - M",
"synced": true,
"variant_id": 8040,
"retail_price": "22.50",
"currency": "USD",
"product": {
"variant_id": 8040,
"product_id": 224,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8040_1550478002.jpg",
"name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / M)"
},
"files": [
{
"id": 120674338,
"type": "default",
"hash": "6238a66581153315488397a5788216b2",
"url": null,
"filename": "logo2.png",
"mime_type": "image/png",
"size": 163254,
"width": 1380,
"height": 1380,
"dpi": null,
"status": "ok",
"created": 1560465358,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
"visible": true
},
{
"id": 120686478,
"type": "preview",
"hash": "fa1a4b2f561f28529f4230ba49eca1db",
"url": null,
"filename": "mockup-79b7c377.png",
"mime_type": "image/png",
"size": 113878,
"width": 600,
"height": 600,
"dpi": 43,
"status": "ok",
"created": 1560469826,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
"visible": false
}
],
"options": []
},
{
"id": 1335801690,
"external_id": "5d02e1420b41f3",
"sync_product_id": 118425517,
"name": "Women's Crop Top - L",
"synced": true,
"variant_id": 8041,
"retail_price": "22.50",
"currency": "USD",
"product": {
"variant_id": 8041,
"product_id": 224,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8041_1550478000.jpg",
"name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / L)"
},
"files": [
{
"id": 120674338,
"type": "default",
"hash": "6238a66581153315488397a5788216b2",
"url": null,
"filename": "logo2.png",
"mime_type": "image/png",
"size": 163254,
"width": 1380,
"height": 1380,
"dpi": null,
"status": "ok",
"created": 1560465358,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
"visible": true
},
{
"id": 120686478,
"type": "preview",
"hash": "fa1a4b2f561f28529f4230ba49eca1db",
"url": null,
"filename": "mockup-79b7c377.png",
"mime_type": "image/png",
"size": 113878,
"width": 600,
"height": 600,
"dpi": 43,
"status": "ok",
"created": 1560469826,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
"visible": false
}
],
"options": []
},
{
"id": 1335801691,
"external_id": "5d02e1420b4238",
"sync_product_id": 118425517,
"name": "Women's Crop Top - XL",
"synced": true,
"variant_id": 8042,
"retail_price": "22.50",
"currency": "USD",
"product": {
"variant_id": 8042,
"product_id": 224,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8042_1550478006.jpg",
"name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / XL)"
},
"files": [
{
"id": 120674338,
"type": "default",
"hash": "6238a66581153315488397a5788216b2",
"url": null,
"filename": "logo2.png",
"mime_type": "image/png",
"size": 163254,
"width": 1380,
"height": 1380,
"dpi": null,
"status": "ok",
"created": 1560465358,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
"visible": true
},
{
"id": 120686478,
"type": "preview",
"hash": "fa1a4b2f561f28529f4230ba49eca1db",
"url": null,
"filename": "mockup-79b7c377.png",
"mime_type": "image/png",
"size": 113878,
"width": 600,
"height": 600,
"dpi": 43,
"status": "ok",
"created": 1560469826,
"thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
"preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
"visible": false
}
],
"options": []
}
]
},
"extra": []
}
I'm trying to access elements from files like this :
foreach($variants['result'] as $variant){
//access file images
$imgurl = $variant['files']['preview_url'];
echo '<img src="$imgurl">';
echo '<br>';
}
I get errors :
Notice: Undefined index: files
Notice: Undefined index: preview_url
Can someone explain to me what I am not understanding please?
My guess is that, there are multiple images, not sure which one would be desired, it could be any of these:
$variant['files'][1]['preview_url']
or
$variant['files'][0]['preview_url']
Test
$variants = json_decode($variants, true);
$html = '';
foreach ($variants['result']["sync_variants"] as $variant) {
$html .= '<img src="' . $variant['files'][1]['preview_url'] . '"><br>';
}
echo $html;
Output
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
This code should help. Problem was connected with additional sub array.
foreach($variants['result']['sync_variants'] as $variant) {
foreach($variant['files'] as $file) {
//access file images
$imgurl = $file['preview_url'];
echo '<img src="' . $imgurl . '">';
echo '<br>';
}
}
Answer to the comment:
foreach($variants['result']['sync_variants'] as $variant) {
foreach($variant['files'] as $file) {
if ($file['type'] != 'preview') {
continue;
}
//access file images
$imgurl = $file['preview_url'];
echo '<img src="' . $imgurl . '">';
echo '<br>';
}
}
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 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 />";
}
}
}
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
},
...]
}