Grab/Fetch Facebook comments through query.using Graph API - php
So I was trying to fetch facebook comments then put it into my own website so then I could reply it through my website. After doing some research, I found it Graph API could be the solution to my problem. Below is the code
<?php
$group_id ="14********_1**********99";
$access_token="accesstoken";
$url = "https://graph.facebook.com/v2.11/" . $group_id .
"/comments?
fields=message,from{name,id}&access_token=".$access_token;
$json= file_get_contents($url);
$data = json_decode($json);
foreach ($data->comments->data as $key) {
if(isset($key->message))
{
echo $key->message;
echo "<br>";
}
}
?>
How do I fetch comments from Facebook through the URL above? Sorry I'm still new to programming.
Related
Google contacts api parsing xml and get contact title
I'm using oauth 2.0 and I trying to use contacts google api. $url = 'https://www.google.com/m8/feeds/contacts/default/full?v=2.0&max-results='.$max_results.'&oauth_token='.$accesstoken; Result of this I have there: $xmlresponse result is fine and good (it means that not have issues) $xml = new SimpleXMLElement($xmlresponse); $xml->registerXPathNamespace('atom', 'http://schemas.google.com/g/2005#kind'); $result = $xml->xpath('//atom:title'); var_dump($xml); var_dump($result); foreach ($result as $title) { echo $title->attributes()->address . "<br>"; } I want to get title contact (in var_dump($xml) it is visible as 'title') but $result is empty Question: 1) How to get title contact from this xml response ?
How extract information from a json retrieved from an API?
I'm trying to get somes informations from an API but I always failed. The API code : {"matches":[{"matchId":2032325149,"region":"EUW","platformId":"EUW1","matchMode":"CLASSIC","matchType":"MATCHED_GAME","matchCreation":1427400448291,"matchDuration":2225,"queueType":"RANKED_SOLO_5x5","mapId":11,"season":"SEASON2015","matchVersion":"5.6.0.190","participants":[{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":53,"highestAchievedSeasonTier":"UNRANKED","timeline":{"creepsPerMinDeltas":{"zeroToTen":0.9,"tenToTwenty":0.9,"twentyToThirty":0.6,"thirtyToEnd":1.2},"xpPerMinDeltas":{"zeroToTen":253.60000000000002,"tenToTwenty":237.8,"twentyToThirty":477.29999999999995,"thirtyToEnd":448.4},"goldPerMinDeltas":{"zeroToTen":128.2,"tenToTwenty":169.6,"twentyToThirty":310.9,"thirtyToEnd":211.2},"csDiffPerMinDeltas":{"zeroToTen":-0.8,"tenToTwenty":-0.5499999999999998,"twentyToThirty":-1.2499999999999998,"thirtyToEnd":-0.30000000000000027},"xpDiffPerMinDeltas":{"zeroToTen":-12.299999999999983,"tenToTwenty":-127.04999999999998,"twentyToThirty":120.09999999999997,"thirtyToEnd":-491.9},"damageTakenPerMinDeltas":{"zeroToTen":187.2,"tenToTwenty":644.0999999999999,"twentyToThirty":1015.2,"thirtyToEnd":2076.2},"damageTakenDiffPerMinDeltas":{"zeroToTen":9.149999999999963,"tenToTwenty":32.49999999999997,"twentyToThirty":126.24999999999991,"thirtyToEnd":762.9000000000001},"role":"DUO_SUPPORT","lane":"BOTTOM"},"masteries":[{"masteryId":4211,"rank":2},{"masteryId":4213,"rank":2},{"masteryId":4221,"rank":1},{"masteryId":4222,"rank":3},{"masteryId":4232,"rank":1},{"masteryId":4233,"rank":3},{"masteryId":4234,"rank":3},{"masteryId":4244,"rank":1},{"masteryId":4312,"rank":3},{"masteryId":4313,"rank":3},{"masteryId":4322,"rank":1},{"masteryId":4324,"rank":1},{"masteryId":4331,"rank":3},{"masteryId":4334,"rank":1},{"masteryId":4341,"rank":1},{"masteryId":4342,"rank":1}],"stats":{"winner":false,"champLevel":15,"item0":2049,"item1":3401,"item2":3270,"item3":2010,"item4":3110,"item5":3082,"item6":3340,"kills":1,"doubleKills":0,"tripleKills":0,"quadraKills":0,"pentaKills":0,"unrealKills":0,"largestKillingSpree":0,"deaths":11,"assists":10,"totalDamageDealt":31311,"totalDamageDealtToChampions":9198,"totalDamageTaken":31747,"largestCriticalStrike":0,"totalHeal":3600,"minionsKilled":36,"neutralMinionsKilled":0,"neutralMinionsKilledTeamJungle":0,"neutralMinionsKilledEnemyJungle":0,"goldEarned":8264,"goldSpent":8055,"combatPlayerScore":0,"objectivePlayerScore":0,"totalPlayerScore":0,"totalScoreRank":0,"magicDamageDealtToChampions":7159,"physicalDamageDealtToChampions":2038,"trueDamageDealtToChampions":0,"visionWardsBoughtInGame":0,"sightWardsBoughtInGame":0,"magicDamageDealt":22000,"physicalDamageDealt":5572,"trueDamageDealt":3737,"magicDamageTaken":15037,"physicalDamageTaken":16608,"trueDamageTaken":102,"firstBloodKill":false,"firstBloodAssist":false,"firstTowerKill":false,"firstTowerAssist":false,"firstInhibitorKill":false,"firstInhibitorAssist":false,"inhibitorKills":0,"towerKills":0,"wardsPlaced":28,"wardsKilled":1,"largestMultiKill":1,"killingSprees":0,"totalUnitsHealed":4,"totalTimeCrowdControlDealt":445},"participantId":0,"runes":[{"runeId":5273,"rank":9},{"runeId":5289,"rank":9},{"runeId":5317,"rank":9},{"runeId":5357,"rank":3}]}],"participantIdentities":[{"participantId":0,"player":{"summonerId":66366116,"summonerName":"name","matchHistoryUri":"/v1/stats/player_history/EUW1/217404427","profileIcon":690}}]}]} I'm trying to get "Kills" value and this is my php code : <?php $result = file_get_contents('API URL LINK HERE); $key = json_decode($result); print '<p><b>' . $key->matches->participants->stats->Kills . '</b></p>'; ?> Please can someone correct my code ? Thanks a lot.
<?php $json = '{"matches":[{"matchId":2032325149,"region":"EUW","platformId":"EUW1","matchMode":"CLASSIC","matchType":"MATCHED_GAME","matchCreation":1427400448291,"matchDuration":2225,"queueType":"RANKED_SOLO_5x5","mapId":11,"season":"SEASON2015","matchVersion":"5.6.0.190","participants":[{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":53,"highestAchievedSeasonTier":"UNRANKED","timeline":{"creepsPerMinDeltas":{"zeroToTen":0.9,"tenToTwenty":0.9,"twentyToThirty":0.6,"thirtyToEnd":1.2},"xpPerMinDeltas":{"zeroToTen":253.60000000000002,"tenToTwenty":237.8,"twentyToThirty":477.29999999999995,"thirtyToEnd":448.4},"goldPerMinDeltas":{"zeroToTen":128.2,"tenToTwenty":169.6,"twentyToThirty":310.9,"thirtyToEnd":211.2},"csDiffPerMinDeltas":{"zeroToTen":-0.8,"tenToTwenty":-0.5499999999999998,"twentyToThirty":-1.2499999999999998,"thirtyToEnd":-0.30000000000000027},"xpDiffPerMinDeltas":{"zeroToTen":-12.299999999999983,"tenToTwenty":-127.04999999999998,"twentyToThirty":120.09999999999997,"thirtyToEnd":-491.9},"damageTakenPerMinDeltas":{"zeroToTen":187.2,"tenToTwenty":644.0999999999999,"twentyToThirty":1015.2,"thirtyToEnd":2076.2},"damageTakenDiffPerMinDeltas":{"zeroToTen":9.149999999999963,"tenToTwenty":32.49999999999997,"twentyToThirty":126.24999999999991,"thirtyToEnd":762.9000000000001},"role":"DUO_SUPPORT","lane":"BOTTOM"},"masteries":[{"masteryId":4211,"rank":2},{"masteryId":4213,"rank":2},{"masteryId":4221,"rank":1},{"masteryId":4222,"rank":3},{"masteryId":4232,"rank":1},{"masteryId":4233,"rank":3},{"masteryId":4234,"rank":3},{"masteryId":4244,"rank":1},{"masteryId":4312,"rank":3},{"masteryId":4313,"rank":3},{"masteryId":4322,"rank":1},{"masteryId":4324,"rank":1},{"masteryId":4331,"rank":3},{"masteryId":4334,"rank":1},{"masteryId":4341,"rank":1},{"masteryId":4342,"rank":1}],"stats":{"winner":false,"champLevel":15,"item0":2049,"item1":3401,"item2":3270,"item3":2010,"item4":3110,"item5":3082,"item6":3340,"kills":1,"doubleKills":0,"tripleKills":0,"quadraKills":0,"pentaKills":0,"unrealKills":0,"largestKillingSpree":0,"deaths":11,"assists":10,"totalDamageDealt":31311,"totalDamageDealtToChampions":9198,"totalDamageTaken":31747,"largestCriticalStrike":0,"totalHeal":3600,"minionsKilled":36,"neutralMinionsKilled":0,"neutralMinionsKilledTeamJungle":0,"neutralMinionsKilledEnemyJungle":0,"goldEarned":8264,"goldSpent":8055,"combatPlayerScore":0,"objectivePlayerScore":0,"totalPlayerScore":0,"totalScoreRank":0,"magicDamageDealtToChampions":7159,"physicalDamageDealtToChampions":2038,"trueDamageDealtToChampions":0,"visionWardsBoughtInGame":0,"sightWardsBoughtInGame":0,"magicDamageDealt":22000,"physicalDamageDealt":5572,"trueDamageDealt":3737,"magicDamageTaken":15037,"physicalDamageTaken":16608,"trueDamageTaken":102,"firstBloodKill":false,"firstBloodAssist":false,"firstTowerKill":false,"firstTowerAssist":false,"firstInhibitorKill":false,"firstInhibitorAssist":false,"inhibitorKills":0,"towerKills":0,"wardsPlaced":28,"wardsKilled":1,"largestMultiKill":1,"killingSprees":0,"totalUnitsHealed":4,"totalTimeCrowdControlDealt":445},"participantId":0,"runes":[{"runeId":5273,"rank":9},{"runeId":5289,"rank":9},{"runeId":5317,"rank":9},{"runeId":5357,"rank":3}]}],"participantIdentities":[{"participantId":0,"player":{"summonerId":66366116,"summonerName":"name","matchHistoryUri":"/v1/stats/player_history/EUW1/217404427","profileIcon":690}}]}]}'; $data = json_decode($json, true); echo $data['matches'][0]['participants'][0]['stats']['kills']; ?>
How can I fetch data from facebook events using graph API
I want to fetch data from a facebook event using graph API. I wrote the following code : <?php $url = 'https://graph.facebook.com/events/1588093858105216'; $contents = file_get_contents( $url ); if( $contents ) { $data = json_decode( $contents, true ); echo '<pre>'; print_r( $data ); echo '</pre>'; } ?> My event page address URL is https://www.facebook.com/events/1588093858105216/. The above code is not working properly. When I am using the same code for a page,e.g., https://www.facebook.com/TangeloTown , it works fine. <?php $url = 'https://graph.facebook.com/TangeloTown'; $contents = file_get_contents( $url ); if( $contents ) { $data = json_decode( $contents, true ); echo '<pre>'; print_r( $data ); echo '</pre>'; } ?> Also, I am unable to run it on localhost. Please help me to solve this or please tell me any other method to solve this.
Generally, you need an access token to access all graph api. Please check the doc here for event api: https://developers.facebook.com/docs/graph-api/reference/v2.3/event In your case, if you want to access event, you need a user access token or app access token. To get user access token you check this doc: https://developers.facebook.com/docs/facebook-login/access-tokens in this way, you need to ask user for authorization. But if you only want to access public event, you can simply use your app access token, which can be found here: https://developers.facebook.com/tools/accesstoken/, but the prerequisite is that you need to register a FB app.
How to get count of likes,shares,comment for individual post in facebook using open graph?
I am using Open graph to get face-book page contents.The issue is that for anonymous user accessing the graph result its showing count of likes,shares and comments to be zero for each post. This is the code which I using for php. $graph_url = "http://graph.facebook.com?id=".urlencode($feed_url); $content = json_decode(file_get_contents($graph_url)); $account_url = "https://www.facebook.com/feeds/page.php?id=".$content->id."&format=json"; Here content->id is the facebook page id . Is there any way of getting count of likes,shares and comment for each post with anonymous user itself. Any help will be much appreciated.Thanks in advance.
You cannot access private data as anonymous user. You need an access-token (FB-oAuth) in case with a Facebook Application and Users/Page/App permissions to access private data. You can try it with fql or graph API. If you try to access "private" data. Make sure your created a facebook app before and unlock all user permissions you need to. Means, Stream Reading I think: https://developers.facebook.com/docs/facebook-login/permissions/v2.2. Without that permission, you cannot access private data. Public example: http://graph.facebook.com/386050065267_10153256675935268/comments?summary=true SELECT like_info.like_count, comment_info.comment_count, share_count FROM stream WHERE post_id IN ( SELECT concat(id,'_', substr("https://www.facebook.com/Macklemore/posts/10153256675935268", strpos("https://www.facebook.com/Macklemore/posts/10153256675935268", '/posts')+7, strlen("https://www.facebook.com/Macklemore/posts/10153256675935268"))) FROM profile WHERE username IN ( SELECT substr(url, strpos(url, 'facebook.com/')+13, strpos(url, '/posts')-strpos(url, 'facebook.com/')-13) FROM object_url WHERE url = "https://www.facebook.com/Macklemore/posts/10153256675935268")) Result: { "data": [ { "like_info": { "like_count": 5506 }, "comment_info": { "comment_count": 353 }, "share_count": 392 } ] } Public data can be fetched like: http://graph.facebook.com/386050065267_10153256675935268/comments?summary=true or https://www.facebook.com/PAGE_ID/posts/POST_ID
enter code here <?php $fql = "SELECT share_count, like_count, comment_count "; $fql .= " FROM link_stat WHERE url = '$url'"; $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql); // Facebook Response is in JSON $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$fqlURL); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $output = curl_exec($ch); curl_close($ch); $facebookdata json_decode($output); echo $facebookdata[0]->share_count; echo "<br>"; // facebook like count echo $facebookdata[0]->like_count; echo " count <br>"; // facebook comment count echo $facebookdata[0]->comment_count; It's very easy please try it
Twitter API v 1.1 code in PHP
I am using twitter API v1.1 to get tweets in my web application the code for getting tweets is as $name = 'zkalvi'; $url = "https://api.twitter.com/1.1/search/tweets.json/".$name; $tweets = json_decode(file_get_contents($url)); echo '<ul>'; foreach($tweets as $tweet) { echo '<li>'.$tweet->text.'</li>'; } echo '</ul>'; and $name = 'zkalvi'; $url = "https://api.twitter.com/1.1/search/tweets.json/".$name; $tweets = simplexml_load_file($url); echo '<ul>'; foreach($tweets as $tweet) { echo '<li>'.$tweet->text.'</li>'; } echo '</ul>'; both of these codes are not showing the tweets. Please help me why the tweets are not showing and how could I show the tweets? Thanks in advance.
first of all, the way you are calling is wrong, your url should be like this https://api.twitter.com/1.1/search/tweets.json?q=zkalvi. By this url you will get following output {"errors":[{"message":"Bad Authentication data","code":215}]} This is because , in twitter 1.1 version, most of thing needs authentication. So better you use twitter server side library for getting results. Here is link for twitteroauth library for php twitteroauth