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
}
Related
I got a response like this now i want to print
[products] how to print
i try this print_r($response['products']);
My Response
stdClass Object
(
[users] => Array
(
[0] => stdClass Object
(
[username] => admin
[firstName] => admin
[lastName] => admin
[products] => Array
(
[0] => stdClass Object
(
[name] => 1
[url] => mytest.html
)
[1] => stdClass Object
(
[name] => 2
[url] => mytest.html
)
[2] => stdClass Object
(
[name] => 3
[url] => mytest.html
)
[3] => stdClass Object
(
[name] => 4
[url] => mytest.html
)
[4] => stdClass Object
(
[name] => 5
[url] => mytest.html
)
[5] => stdClass Object
(
[name] => 6
[url] => mytest.html
)
[6] => stdClass Object
(
[name] => 7
[url] => mytest.html
)
)
)
)
[error] =>
[errorMessage] =>
)
Try This one
print_r($response->users['0']->products);
I am attempting to parse the Instagram API standard object that looks like this:
stdClass Object
(
[pagination] => stdClass Object
(
[next_url] => https://api.instagram.com/v1/users /283549787/media/recent?access_token=3036118427.1677ed0.fceea7d5e10440b691a670ddb6a97783&max_id=1059902719332635377_283549787
[next_max_id] => 1059902719332635377_283549787
)
[meta] => stdClass Object
(
[code] => 200
)
[data] => Array
(
[0] => stdClass Object
(
[attribution] =>
[tags] => Array
(
[0] => vbb
[1] => 100k
[2] => ig
)
[type] => image
[location] =>
[comments] => stdClass Object
(
[count] => 22
[data] => Array
(
[0] => stdClass Object
(
[created_time] => 1461241283
[text] => weeeelll 💥
[from] => stdClass Object
(
[username] => thewolfcult
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519175_228551560821189_1928102794_a.jpg
[id] => 2685463016
[full_name] => Wolf Cult
)
[id] => 17846791729091814
)
[1] => stdClass Object
(
[created_time] => 1461241621
[text] => this pic is LOL 😂😂😂
[from] => stdClass Object
(
[username] => class_y_man
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/13129374_610084285824464_241975035_a.jpg
[id] => 189866780
[full_name] => Z.k
)
[id] => 17846791822091814
)
[2] => stdClass Object
(
[created_time] => 1461241649
[text] => Love it ðŸ˜
[from] => stdClass Object
(
[username] => kosmonelle
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/13113731_1661552477439373_875045798_a.jpg
[id] => 2371520392
[full_name] =>
)
[id] => 17846791834091814
)
[3] => stdClass Object
(
[created_time] => 1461254879
[text] => ðŸ†ðŸ‘
[from] => stdClass Object
(
[username] => modelsandphotographers1
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11336128_1774042986155389_99539322_a.jpg
[id] => 1418124928
[full_name] => Best Models & Photographers
)
[id] => 17846795935091814
)
)
)
[filter] => Normal
[created_time] => 1461230731
[link] => https://www.instagram.com/p/BEdQrSDytME/
[likes] => stdClass Object
(
[count] => 16672
[data] => Array
(
[0] => stdClass Object
(
[username] => jassi_bains_official
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/12327958_458252344360785_973161765_a.jpg
[id] => 2302067650
[full_name] => Jassi bains
)
[1] => stdClass Object
(
[username] => ukay.tlb527
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg
[id] => 3101669439
[full_name] => Melvin Rosetta
)
[2] => stdClass Object
(
[username] => hahmedhossam121414411
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg
[id] => 3150267725
[full_name] => Bobby Ivan
)
[3] => stdClass Object
(
[username] => somi__0
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/13102392_989473131142748_1189753865_a.jpg
[id] => 2190016506
[full_name] => Somi 👸ðŸ¼ðŸ’•
)
)
)
[images] => stdClass Object
(
[low_resolution] => stdClass Object
(
[url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/12918470_1000175006702614_1067887222_n.jpg?ig_cache_key=MTIzMzIxNTIxNjA0MjAzODAyMA%3D%3D.2.l
[width] => 320
[height] => 320
)
[thumbnail] => stdClass Object
(
[url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/c190.0.644.644/12918387_1540009256304246_510188281_n.jpg?ig_cache_key=MTIzMzIxNTIxNjA0MjAzODAyMA%3D%3D.2.c
[width] => 150
[height] => 150
)
[standard_resolution] => stdClass Object
(
[url] => https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/12918470_1000175006702614_1067887222_n.jpg?ig_cache_key=MTIzMzIxNTIxNjA0MjAzODAyMA%3D%3D.2.l
[width] => 640
[height] => 640
)
)
[users_in_photo] => Array
(
)
[caption] => stdClass Object
(
[created_time] => 1461230731
[text] => really , thank you 🆠#ig #100k #vbb
[from] => stdClass Object
(
[username] => mattiacanfa
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/13113786_488414981352515_1701597009_a.jpg
[id] => 283549787
[full_name] => Mattia Canfarotta
)
[id] => 17846789149091814
)
[user_has_liked] =>
[id] => 1233215216042038020_283549787
[user] => stdClass Object
(
[username] => mattiacanfa
[profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/s150x150/13113786_488414981352515_1701597009_a.jpg
[id] => 283549787
[full_name] => Mattia Canfarotta
)
)
)
I am trying to get standard_resolution url like this with $json_result being the object:
foreach ($json_result->data->images->standard_resolution->url as $images){
print_r($images);
}
For some reason nothing is being returned.
I was able to resolve because data is an array it needs to be this:
foreach ($json_result->data as $data){
foreach ($data->images->standard_resolution as $link){
echo "<img src='$link'></img>";
}
}
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.
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!
}
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'];