Get an undefined property error when fetching data from a JSON string?
I have the following JSON string
{
"data": [
{
"id": "533513150124231",
"created_time": "2015-02-27T05:23:02+0000",
"photos": {
"data": [
{
"images": [
{
"height": 667,
"source": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10418414_555711037904442_8567793956228056677_n.jpg?oh=a8815f978e43abb98ab229cf83c85264&oe=559AB81A&__gda__=1440316102_f98baa62cfba9d6f94cd327f682ffd25",
"width": 460
},
{
"height": 464,
"source": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/p320x320/10418414_555711037904442_8567793956228056677_n.jpg?oh=d35e77306662faecf87e744605d307bd&oe=559A52BE&__gda__=1436229856_60f872b53adcbf8aa302903fb019b327",
"width": 320
},
{
"height": 540,
"source": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/p180x540/10418414_555711037904442_8567793956228056677_n.jpg?oh=b759bdb9362c6bfee5aa2709928d8f69&oe=559CEC03&__gda__=1436252765_64a5b0e1c663efead53300007c437990",
"width": 372
},
{
"height": 188,
"source": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/p130x130/10418414_555711037904442_8567793956228056677_n.jpg?oh=cde013e3d7e9b2433e57b66e33167d92&oe=55A6CED1&__gda__=1436392591_2d5580f28f551316f45bc3930c459386",
"width": 130
},
{
"height": 225,
"source": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/p75x225/10418414_555711037904442_8567793956228056677_n.jpg?oh=a5caf9c528ba7a8f8380db27d42f82c0&oe=55E49852&__gda__=1436028283_98c7a7d639ed37620f3a9b0fbb78472e",
"width": 155
}
],
"created_time": "2015-04-17T06:00:00+0000",
"id": "555711037904442"
}
],
"paging": {
"cursors": {
"before": "NTU1NzExMDM3OTA0NDQy",
"after": "NTU1NzExMDM3OTA0NDQy"
},
"next": "https://graph.facebook.com/v2.3/533513150124231/photos?pretty=0&fields=images&limit=1&after=NTU1NzExMDM3OTA0NDQy"
}
}
},
{
"id": "533911933417686",
"created_time": "2015-02-28T07:18:09+0000",
"photos": {
"data": [
{
"images": [
{
"height": 960,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/11156332_555538204588392_7524203027467514208_n.jpg?oh=472d4dc677b7c6a1358bf59392071039&oe=55A97829&__gda__=1436014225_b6c761b77d7659f3499c98fa870efdd7",
"width": 960
},
{
"height": 720,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p720x720/11156332_555538204588392_7524203027467514208_n.jpg?oh=e9a1b55146bf65d1779fcfabe063012e&oe=55D62F58&__gda__=1437234622_80c2bf1dbec64f6851b15b9d33084b21",
"width": 720
},
{
"height": 600,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p600x600/11156332_555538204588392_7524203027467514208_n.jpg?oh=0126ec5adea74e13252a3bafaba14a02&oe=55A67B37&__gda__=1440779729_3645ddf066d0158821f83f8ea077b05f",
"width": 600
},
{
"height": 480,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p480x480/11156332_555538204588392_7524203027467514208_n.jpg?oh=2d60610a0e588be12333f1c89d9a2317&oe=559A8CD7&__gda__=1435982897_782ac7a2cc1c6cafac63aaef4a29ae4f",
"width": 480
},
{
"height": 320,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p320x320/11156332_555538204588392_7524203027467514208_n.jpg?oh=4148aaca5b81862a626b5399db291ba9&oe=559F338D&__gda__=1440297579_389092cfce5f8aafef4573968409e819",
"width": 320
},
{
"height": 540,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p180x540/11156332_555538204588392_7524203027467514208_n.jpg?oh=d4f772948708181ba7004e7f324988c9&oe=55E13930&__gda__=1436695254_4f2c082572f0241510ab59504fadfc3e",
"width": 540
},
{
"height": 130,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p130x130/11156332_555538204588392_7524203027467514208_n.jpg?oh=e095b6eaa807792afbfeaa2ed85f276b&oe=55A501E2&__gda__=1437193220_18d71223bd95cf5cba2542a11c24d18e",
"width": 130
},
{
"height": 225,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p75x225/11156332_555538204588392_7524203027467514208_n.jpg?oh=8e3f487af88e40470b93073853e3e8c7&oe=55A95661&__gda__=1436678326_211dc38936f310d46801ffea0582a646",
"width": 225
}
],
"created_time": "2015-04-16T16:40:57+0000",
"id": "555538204588392"
}
],
"paging": {
"cursors": {
"before": "NTU1NTM4MjA0NTg4Mzky",
"after": "NTU1NTM4MjA0NTg4Mzky"
},
"next": "https://graph.facebook.com/v2.3/533911933417686/photos?pretty=0&fields=images&limit=1&after=NTU1NTM4MjA0NTg4Mzky"
}
}
},
{
"id": "533471226795090",
"created_time": "2015-02-27T03:20:20+0000",
"photos": {
"data": [
{
"images": [
{
"height": 252,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10384022_533471210128425_1090655257311798538_n.png?oh=42293c184f93099fed226845661e68ba&oe=55A8A0BC&__gda__=1440376580_7355eccec9a71b9c1b1c3fb08b7c438d",
"width": 785
},
{
"height": 130,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p130x130/10384022_533471210128425_1090655257311798538_n.png?oh=61e2595ba9367c63ce4c31272d2f5965&oe=55E1AE77&__gda__=1440510353_4d66bcf977355eb4514d6ef5b665f496",
"width": 404
},
{
"height": 225,
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p75x225/10384022_533471210128425_1090655257311798538_n.png?oh=331aad239450d4f3d7f6984bae23a2e6&oe=55E237F4&__gda__=1440632867_d90a8c45baa7d4d4cdaa87346095b863",
"width": 700
}
],
"created_time": "2015-02-27T03:20:21+0000",
"id": "533471210128425"
}
],
"paging": {
"cursors": {
"before": "NTMzNDcxMjEwMTI4NDI1",
"after": "NTMzNDcxMjEwMTI4NDI1"
}
}
}
},
{
"id": "533470536795159",
"created_time": "2015-02-27T03:16:54+0000",
"photos": {
"data": [
{
"images": [
{
"height": 320,
"source": "https://scontent.xx.fbcdn.net/hphotos-xfa1/v/t1.0-9/10431515_533470543461825_4940061426497683241_n.jpg?oh=b7c513e31c9dc4da9e64df4094280a70&oe=559D578E",
"width": 240
},
{
"height": 173,
"source": "https://scontent.xx.fbcdn.net/hphotos-xfa1/v/t1.0-9/p130x130/10431515_533470543461825_4940061426497683241_n.jpg?oh=d4d3e16d0cc7b43e9ae44df494f08e29&oe=55D70A45",
"width": 130
},
{
"height": 225,
"source": "https://scontent.xx.fbcdn.net/hphotos-xfa1/v/t1.0-9/p75x225/10431515_533470543461825_4940061426497683241_n.jpg?oh=5d8e3097a633df2c9ca86de0be65622f&oe=5599C4C6",
"width": 168
}
],
"created_time": "2015-02-27T03:16:55+0000",
"id": "533470543461825"
}
],
"paging": {
"cursors": {
"before": "NTMzNDcwNTQzNDYxODI1",
"after": "NTMzNDcwNTQzNDYxODI1"
}
}
}
}
],
"paging": {
"cursors": {
"after": "NTMzNDcwNTM2Nzk1MTU5",
"before": "NTMzNTEzMTUwMTI0MjMx"
}
}
}
I got this string from the Facebook Graph API browser. After that, I tried to extract the image source from the JSON data. Here is the code I wrote.
$output = 'All the JSON data above is written here';
$output = json_decode($output);
for($i=0;$i<2;$i++)
{
$image = $output->photos->data->images[$i][0]->source;
echo $image.'</br>';
}
With this code I am getting the following error
Undefined property: stdClass::$photos on line 1796
Trying to get property of non-object on line 1796
I want to extract the source for the first image in each category such as
https://www.facebook.com/l.php?u=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fhphotos-xfa1%2Fv%2Ft1.0-9%2F10431515_533470543461825_4940061426497683241_n.jpg%3Foh%3Db7c513e31c9dc4da9e64df4094280a70%26oe%3D559D578E&h=8AQE_FhcA
+3
source to share
2 answers
I suggest an alternative with foreach
. You can also use it to traverse the decoded JSON:
foreach($output->data as $data) {
$photos = $data->photos->data[0]->images;
foreach($photos as $img) {
echo "{$img->source} <br/>";
}
}
By the way, this goes for all source packages, you would like to use the first one, you can use reset
to get the first one in the current batch and then point to the property:
foreach($output->data as $data) {
$photos = $data->photos->data[0]->images;
$first = reset($photos)->source;
echo "{$first} <br/>";
}
+1
source to share