make a variable for each item from facebook fql json output - php

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";

Related

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
}

Foreach PHP facebook API

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;
}

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'];

How to access object data and display the comments

How do I access the comments and display them?
stdClass Object
(
[stream] => stdClass Object
(
[id] => 521734635_403433313001586
[from] => stdClass Object
(
[name] => Jonathan Rosario
[id] => 521734635
)
[message] => What if we do the same action in our country?...i just hope they be fair. Its just unfair that they also ban our Bibles while we never ban their Qu'rans in our country.
[comments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 521734635_403433313001586_90696065
[from] => stdClass Object
(
[name] => ''
[id] => 570761262
)
[message] => test
[created_time] => 2012-03-29T10:01:49+0000
)
[1] => stdClass Object
(
[id] => 521734635_403433313001586_90696070
[from] => stdClass Object
(
[name] => Alwaynne Kevin
[id] => 100000364267158
)
[message] => Oo nga...bakit ganyan sila...hahah.!
[created_time] => 2012-03-29T10:07:52+0000
)
[2] => stdClass Object
(
[id] => 521734635_403433313001586_90696146
[from] => stdClass Object
(
[name] => Jp Peña
[id] => 100000599584313
)
[message] => they're just doing what the "christian" crusaders did to them.
[created_time] => 2012-03-29T10:33:12+0000
)
)
[count] => 3
)
[picture] => http://external.ak.fbcdn.net/safe_image.php?d=AQCr7a57qDUU2MHR&w=90&h=90&url=http%3A%2F%2Fwww.interaksyon.com%2Fassets%2Fimages%2Farticles%2Finterphoto_1332546902.jpg
[name] => Top Saudi cleric 'issues fatwa' on Christian churches; Bishops alarmed - InterAksyon.com
[description] => Christian bishops are sharply criticizing Saudi Arabia's top religious official after reports that he issued a fatwa saying all churches on the Arabian Peninsula should be destroyed. At least 3.5 million Christians live in the Gulf Arab region. They are mostly Catholic workers from the Philippines a...
[created_time] => 2012-03-29T10:00:41+0000
)
)
you got the whole list of objects in comments so give try following:
foreach( $yourObj->stream->comments as $comment){
echo sprintf('%s: %s', $comment->from->name, $comment->message);
}

Categories