Foreach PHP facebook API - php

I have to the following code which is returned from the Facebook API:
Facebook\GraphNodes\GraphObject Object ( [backingData:protected] => Array ( [data] => Array ( [0] => stdClass Object ( [name] => Mobile Uploads [id] => 680467525306684 [created_time] => 2013-12-09T10:17:39+0000 ) [1] => stdClass Object ( [name] => Timeline Photos [id] => 588255297861241 [created_time] => 2013-06-06T20:12:14+0000 ) [2] => stdClass Object ( [name] => Mercedes CLK55 AMG - Wide Conversion [id] => 732173653469404 [created_time] => 2014-03-20T20:45:17+0000 ) [3] => stdClass Object ( [name] => NISSAN 350Z Colour Change.. [id] => 677937242226379 [created_time] => 2013-12-04T13:38:26+0000 ) [4] => stdClass Object ( [name] => TOYOTA SURF [id] => 675809505772486 [created_time] => 2013-11-29T16:36:08+0000 ) [5] => stdClass Object ( [name] => HONDA CIVIC TYPE R EP3 [id] => 667129319973838 [created_time] => 2013-11-13T16:12:05+0000 ) [6] => stdClass Object ( [name] => Mercedes SL65 Conversion [id] => 661587023861401 [created_time] => 2013-11-02T16:33:48+0000 ) [7] => stdClass Object ( [name] => 2003 Mercedes SL - SL65 Conversion [id] => 661525630534207 [created_time] => 2013-11-02T13:58:05+0000 ) [8] => stdClass Object ( [name] => HONDA FIREBLADE CBR1000 [id] => 657704500916320 [created_time] => 2013-10-26T16:15:56+0000 ) [9] => stdClass Object ( [name] => WIDE ARCHED MK5 GOLF [id] => 621816751171762 [created_time] => 2013-08-17T13:04:41+0000 ) [10] => stdClass Object ( [name] => YAMAHA QUAD BIKE [id] => 588281337858637 [created_time] => 2013-06-06T21:42:58+0000 ) [11] => stdClass Object ( [name] => Cover Photos [id] => 588150447871726 [created_time] => 2013-06-06T15:37:58+0000 ) [12] => stdClass Object ( [name] => AUDI A6 3.0TDI QUATTRO [id] => 588117341208370 [created_time] => 2013-06-06T14:39:59+0000 ) [13] => stdClass Object ( [name] => BMW 535D TWIN TURBO [id] => 588113591208745 [created_time] => 2013-06-06T14:30:07+0000 ) [14] => stdClass Object ( [name] => AUDI A4 SE [id] => 588110747875696 [created_time] => 2013-06-06T14:22:35+0000 ) [15] => stdClass Object ( [name] => Profile Pictures [id] => 588096834543754 [created_time] => 2013-06-06T13:47:27+0000 ) ) [paging] => stdClass Object ( [cursors] => stdClass Object ( [after] => NTg4MDk2ODM0NTQzNzU0 [before] => NjgwNDY3NTI1MzA2Njg0 ) ) ) )
These are photo albums pulled in from a Facebook page. I want to loop through them and get the "name", "ID", etc but it appears a normal foreach function returns the following error:
Fatal error: Cannot use object of type Facebook\GraphNodes\GraphObject as array in /Applications/MAMP/htdocs/git/facebook/index.php on line 29
Here's the full logic:
$request = new FacebookRequest(
$session,
'GET',
'/588091587877612/albums?fields=name'
);
$response = $request->execute();
$albums = $response->getGraphObject();
print_r ($albums);
foreach($albums['data'] as $album) {
echo $album['name'];
}
Does anyone have any idea why that error is being returned?
Thanks in advance,
Connor

Can you try :
$albums = $response->getGraphObject()->asArray();

Looking at the documentation, it looks like you're not handling the data correctly.
Untested, but here's a suggestion:
$albums = $response->getGraphObject();
$albums_data = $albums->getProperty('data');
foreach ($albums_data as $album) {
echo $album->name;
}

Related

How to get detail of instagram post from media id

I have obtained the list of media id. for eg.
$media_id = 1147661250940270414_242644508
Now, as i have saved the media id already. i now want to fetch other detail information instantly using web services. Below is what i have tried but got no luck.
$url = 'https://api.instagram.com/v1/media/'.$media_id.'?access_token='.$myaccess_token;
$response = file_get_contents($url);
$response = json_decode($response);
print_r($response);
Reference from instagram documentation. how could i debug ?
Note : i have enabled curl. The above prints nothing.
Also printing $url and browsing prints json data.
What exactly is wrong for you? It is working perfectly with my access token and your media_id. The result is:
stdClass Object ( [meta] => stdClass Object ( [code] => 200 ) [data] => stdClass Object ( [attribution] => [tags] => Array ( [0] => summer [1] => merrychristmas [2] => love [3] => christmas [4] => family ) [type] => image [location] => [comments] => stdClass Object ( [count] => 0 [data] => Array ( ) ) [filter] => Rise [created_time] => 1451031904 [link] => https://www.instagram.com/p/_tT9PAxndO/ [likes] => stdClass Object ( [count] => 6 [data] => Array ( [0] => stdClass Object ( [username] => nedz3388 [profile_picture] => https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11296865_494353930737804_199912633_a.jpg [id] => 245135480 [full_name] => Nerida Cole ) [1] => stdClass Object ( [username] => ashleyhughesxixii [profile_picture] => https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-19/s150x150/12292658_1933982046828004_675035842_a.jpg [id] => 8121980 [full_name] => AshleyHughes ) [2] => stdClass Object ( [username] => beccao88 [profile_picture] => https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xtp1/t51.2885-19/s150x150/11374242_179260799080196_1224064424_a.jpg [id] => 145580378 [full_name] => Rebecca Ormond ) [3] => stdClass Object ( [username] => love_the_shred [profile_picture] => https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xat1/t51.2885-19/s150x150/10932468_1606801716246599_1519881141_a.jpg [id] => 248027596 [full_name] => Tony Young ) ) ) [images] => stdClass Object ( [low_resolution] => stdClass Object ( [url] => https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/s320x320/e35/12357934_1011207135587426_1185326237_n.jpg [width] => 320 [height] => 320 ) [thumbnail] => stdClass Object ( [url] => https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/s150x150/e35/12357934_1011207135587426_1185326237_n.jpg [width] => 150 [height] => 150 ) [standard_resolution] => stdClass Object ( [url] => https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/s640x640/sh0.08/e35/12357934_1011207135587426_1185326237_n.jpg [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => stdClass Object ( [created_time] => 1451031904 [text] => Merry Christmas to all xx #christmas #merrychristmas #family #love #summer [from] => stdClass Object ( [username] => brooklyne91 [profile_picture] => https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/12276893_1526872547628928_24812970_a.jpg [id] => 242644508 [full_name] => Broooke Ellis ) [id] => 1147661252576048439 ) [user_has_liked] => [id] => 1147661250940270414_242644508 [user] => stdClass Object ( [username] => brooklyne91 [profile_picture] => https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/s150x150/12276893_1526872547628928_24812970_a.jpg [id] => 242644508 [full_name] => Broooke Ellis ) ) )
Then you can split this data individually to how you please.

Likes on Facebook Posts Pagination

I want to count the number of likes (and comments) on a Facebook post. But when there's more than 25 likes on a post, the results are paginated like that:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 802020049912534 [name] => Sagar Lama ) [1] => stdClass Object ( [id] => 590020061101130 [name] => Sheguey Squaad ) [2] => stdClass Object ( [id] => 696207733846316 [name] => Ossama Barcalona ) [3] => stdClass Object ( [id] => 1901218896769448 [name] => Gab Alviola ) [4] => stdClass Object ( [id] => 407069606150738 [name] => Thongloy Thichanon ) [5] => stdClass Object ( [id] => 947084195347741 [name] => Thomas Vujić ) [6] => stdClass Object ( [id] => 1614068872196230 [name] => เพื่อนà¸à¸¹à¹€à¸ˆà¹‡à¸š ชีวิตมึงจบ ) [7] => stdClass Object ( [id] => 1431328280529413 [name] => Ali-Bilal Dernek ) [8] => stdClass Object ( [id] => 855095404568872 [name] => Henry Feng ) [9] => stdClass Object ( [id] => 937460179628960 [name] => Rex Lu ) [10] => stdClass Object ( [id] => 1627484497506023 [name] => Röbërt GärÄïâ ) [11] => stdClass Object ( [id] => 971640236220159 [name] => Reyjoel Jimeno ) [12] => stdClass Object ( [id] => 1644471585776456 [name] => Og Catwalk ) [13] => stdClass Object ( [id] => 1652037021731712 [name] => Rafik Benchibane ) [14] => stdClass Object ( [id] => 499561140196412 [name] => Lizy Smith ) [15] => stdClass Object ( [id] => 10207169299621544 [name] => Ngai Lam ) [16] => stdClass Object ( [id] => 505507699598850 [name] => Ëlloï Gonzalez Sanchez ) [17] => stdClass Object ( [id] => 802078276575586 [name] => Armando Sanchez ) [18] => stdClass Object ( [id] => 10205977306769395 [name] => Fredy Santizo ) [19] => stdClass Object ( [id] => 811409772291367 [name] => Malik Davis ) [20] => stdClass Object ( [id] => 842376069172521 [name] => Isak Renee Martinez ) [21] => stdClass Object ( [id] => 915675738506394 [name] => Kimora Stephens Davis ) [22] => stdClass Object ( [id] => 916531748393520 [name] => Isabel Averill ) [23] => stdClass Object ( [id] => 941049645918044 [name] => Mathias Tenezaca Mendez ) [24] => stdClass Object ( [id] => 1452189318420627 [name] => Andrew Hernandez ) ) [paging] => stdClass Object ( [cursors] => stdClass Object ( [after] => MTQ1MjE4OTMxODQyMDYyNw== [before] => ODAyMDIwMDQ5OTEyNTM0 ) [next] => https://graph.facebook.com/v2.3/15087023444_10153196470723445/likes?access_token=xxxxxxxxxxxxxxxxxxx&limit=25&after=MTQ1MjE4OTMxODQyMDYyNw%3D%3D ) )
How can I count likes and comments on a given post without asking the Facebook API too much. I'm using the PHP SDK
You can either use paging to go through all Likes, or you can use the summary parameter to get a total count:
/post-id?fields=likes.summary(true)
Part of the response:
"summary": {
"total_count": 393,
"can_like": true,
"has_liked": false
}

How to loop through a LinkedIn API response?

I am retrieving connections via LinkedIn's API.
This is a sample of the response. All I want to do is loop through this using PHP, accessing first and last names. Should be simple, but I am stumped. Thanks.
stdClass Object ( [_total] => 420 [values] => Array ( [0] => stdClass
Object ( [apiStandardProfileRequest] => stdClass Object ( [headers] =>
stdClass Object ( [_total] => 1 [values] => Array ( [0] => stdClass
Object ( [name] => x-li-auth-token [value] => name:AnUY ) ) ) [url] =>
http://api.linkedin.com/v1/people/7yw8k_sjqf ) [firstName] => John
[headline] => Finance Manager at ABC. [id] => 7yw8k_sjqf [industry] =>
Insurance [lastName] => Doe [location] => stdClass Object ( [country]
=> stdClass Object ( [code] => us ) [name] => Greater Atlanta Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_73E_PMM3nzAIZLG03bHpPRo3qvCwZL_0DhYpPRogIxiFs7jUYPVRGNRGC1mGegyyqdihoC
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=2633833&authType=name&authToken=AnUY&trk=api*a189561*s197687*
) ) [1] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:tpAo ) ) ) [url] => http://api.linkedin.com/v1/people/FW8obZbI8R
) [firstName] => Jeremy [headline] => Owner, BCD [id] => FW8obZbI8R
[industry] => Computer Software [lastName] => Doe [location] =>
stdClass Object ( [country] => stdClass Object ( [code] => us ) [name]
=> Greater Chicago Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_Lw2NlImcd1e6LmQpL2mIleOMIcf9LeFpbu4IlexHq-R6V2ryWSpZ0HI6LM7rXeaEYf3Gt69H
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=6700159&authType=name&authToken=tpAo&trk=api*a189561*s197687*
) ) [2] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:eETp ) ) ) [url] => http://api.linkedin.com/v1/people/a7-dvlvc7K
) [firstName] => Jane [headline] => Sr. Consultant System Integration
at AT&T [id] => a7-dvlvc7K [industry] => Telecommunications [lastName]
=> Doe [location] => stdClass Object ( [country] => stdClass Object ( [code] => us ) [name] => Greater New York City Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_459zxGU8TBpTZjvisicExTgSSvZCsYXisCGExTR-t1ut5V1_N_6JA37p_Z4gJQqIK653RvmW
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=119413921&authType=name&authToken=eETp&trk=api*a189561*s197687*
) ) [3] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:HjUG ) ) ) [url] => http://api.linkedin.com/v1/people/aie8Zpk9Gm
) [firstName] => John [headline] => Financial Information/Market Data
Professional [id] => aie8Zpk9Gm [industry] => Financial Services
[lastName] => Smith [location] => stdClass Object ( [country] =>
stdClass Object ( [code] => gb ) [name] => Glasgow, United Kingdom )
[pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_N0TLAL3BTlHZCVAbTNTAD03RCjngvsAbCIY-JfOxvg4JldxF9L7MShCp_Avp_ROhKS8LNY
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=1562499&authType=name&authToken=HjUG&trk=api*a189561*s197687*
) ) [4] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:aZIS ) ) ) [url] => http://api.linkedin.com/v1/people/XTGgSuagWI
) [firstName] => Bob [headline] => CEO, Nycomed US at NMO [id] =>
XTGgSuagWI [industry] => Pharmaceuticals [lastName] => brown
[location] => stdClass Object ( [country] => stdClass Object ( [code]
=> us ) [name] => Greater New York City Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_UciCehxmKngbM15je_qytdVG-zSbJrcje_92S7a-DlsFcA1m6iCGyXsjl-uwsq3ybXXbrd1L
[siteStandardProfileRequest] => stdClass Object ( [url] =>
What you have as the response is an stdclass which will have all of its properties public.
This is how you iterate it:
$response = $this->getResponseFromLinkedIn();
foreach ($response->values as $value) {
$firstName = $value->firstName;
$lastName = $value->lastName;
// do something with the extracted values!
}

PHP Facebook API - Access Object Array Item

I have an object array (shown at the bottom) and can access the data by doing this:
echo $MyVariable->id;
echo $MyVariable->type;
and so on. However, I can't access the item [count] by doing this:
echo $MyVariable->id;
Anybody know why? Count seems to be used for something else, a count perhaps?
stdClass Object (
[id] => 573948779291487
[from] => stdClass Object (
[category] => Bar
[category_list] => Array (
[0] => stdClass Object (
[id] => 218693881483234
[name] => Pub
)
[1] => stdClass Object (
[id] => 164049010316507
[name] => Gastropub
) )
[name] => The Melbourne Arms
[id] => 533195070033525
)
[name] => The Melbourne Arms Gallery
[location] => The Melbourne Arms
[place] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
[location] => stdClass Object (
[street] => Main Street
[city] => Melbourne
[country] => United Kingdom
[zip] => YO42 4QJ
[latitude] => 53.8872441
[longitude] => -0.8554097
) )
[link] => http://www.facebook.com/album.php?fbid=573948779291487&id=533195070033525&aid=1073741825
[cover_photo] => 573948812624817
[count] => 28
[type] => normal
[created_time] => 2013-03-13T22:22:45+0000
[updated_time] => 2013-03-13T22:34:40+0000
[can_upload] =>
[likes] => stdClass Object (
[data] => Array (
[0] => stdClass Object (
[id] => 1361671776
[name] => Lesley Maiden
)
[1] => stdClass Object (
[id] => 518580863
[name] => Diane Maiden
)
[2] => stdClass Object (
[id] => 1254875611
[name] => Vanessa Wilson
)
[3] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
)
[4] => stdClass Object (
[id] => 100001664649752
[name] => Bowen Lee
) )
[paging] => stdClass Object (
[next] => https://graph.facebook.com/573948779291487/likes?limit=25&offset=25&__after_id=100001664649752
) ) ) 1
Try handling your data differently
$data = file_get_contents("https://graph.facebook.com/573948779291487");
$data = json_decode($data, true);
echo $data['count'];

make a variable for each item from facebook fql json output

I want to make a variable for each item from facebook fql json output of photo albums. This is my output after I did a json encode.
stdClass Object (
[data] => Array (
[0] => stdClass Object ( [name] => Mobile Uploads )
[1] => stdClass Object ( [name] => Wall Photos )
[2] => stdClass Object ( [name] => Wall Photos )
[3] => stdClass Object ( [name] => New Deck and Patio - DIY )
[4] => stdClass Object ( [name] => New Deck - DIY! )
[5] => stdClass Object ( [name] => Honeymoon )
[6] => stdClass Object ( [name] => 5/5/2012 )
[7] => stdClass Object ( [name] => Cover Photos )
[8] => stdClass Object ( [name] => Profile Pictures )
[9] => stdClass Object ( [name] => Wedding Shower )
[10] => stdClass Object ( [name] => Kuczer Christmas 2011 )
[11] => stdClass Object ( [name] => A Breed Apart studio recording )
[12] => stdClass Object ( [name] => New recessed lighting Kitchen )
[13] => stdClass Object ( [name] => March 10, 2011 Our Engagement )
[14] => stdClass Object ( [name] => Renovated Dining Room )
[15] => stdClass Object ( [name] => Christmas 2010 )
[16] => stdClass Object ( [name] => Florida Trip 2010 )
[17] => stdClass Object ( [name] => MINE )
[18] => stdClass Object ( [name] => Me )
[19] => stdClass Object ( [name] => 2010 FJ Cruiser )
[20] => stdClass Object ( [name] => Family )
[21] => stdClass Object ( [name] => Studio Drums )
[22] => stdClass Object ( [name] => Work )
[23] => stdClass Object ( [name] => A Breed Apart )
[24] => stdClass Object ( [name] => Dirt Biking )
)
)
I want to make a variable of each photo album name such as $variable1 = "Florida Trip 2010" etc...
Assuming you used print_r($fb_result) to generate this output above, you would use $variable1 = $fb_result->data[16]->name to set $variable1 = "Florida Trip 2010";

Categories