How do I get individual Json data from a facebook graph call? - php

Im trying to get shares count from this Facebook graph call using php but am having no luck:
http://graph.facebook.com/?ids=http://google.com
Try 1
$content = file_get_contents("http://graph.facebook.com/?ids=http://google.com");
$parsedJson = json_decode($content, true);
echo $parsedJson['shares'];
Try 2
$content = file_get_contents("http://graph.facebook.com/?ids=http://google.com");
$parsedJson = json_decode($content, true);
echo $parsedJson->shares;
What am I doing wrong?

try
echo $parsedJson["http://google.com"]['shares'];

This will work!
echo ($parsedJson->{'http://google.com'}->shares);

Related

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

get data from external url using php json?

I'm trying to extract data from an external url using json in PHP using the following code:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo $json_data["formatted_address"];
?>
however, I get nothing on my page. in fact, i get this error:
Notice: Undefined index: formatted_address on line 7
is there something I'm missing?
any help would be greatly appreciated.
'formatted_address' is a key of the main array 'results', so you should loop $json_data['results'] and search for the key 'formatted_address'.
try this way,
echo $json_data['results'][0]['formatted_address'];
You are not providing proper INDEX. Proper INDEX is $json_data['results'][0]['formatted_address']; for 1st result.
Use foreach loop to print all address.
Try
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
foreach($json_data['results'] as $item)
{
echo $item['formatted_address']."<br />";
}

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

get data from JSON file (with cURL)

I would like to trnsform some JSON data to a php array, this is my code:
<?php
$obj1=json_decode('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json', true);
$championname = $obj1[data][aatrox][name];
echo $championname;
?>
The problem is that i don't know how to get the data from http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json
How can i make this code work?
Try with file_get_contents() like this:
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json , true);
$championname = $obj1['data']['aatrox']['name'];
echo $championname;
Use file_get_contents to download a file.
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json, true);

Decoding JSON into an array for a xbox live image script?

I'm trying to turn a JSON file of Xbox Live data into variables that I can use in a PHP generated image. My JSON file is here: http://www.xboxgamercard.org/gamercard/test3/xbox.php
I have tried this:
$request_url = 'xbox.php';
$json = file_get_contents($request_url);
$decode = json_decode($json, true);
var_dump($decode['gamertag'][0]);
but it just returns NULL.
I would like to use the JSON as shown here:
$gamertag = $data['Gamertag'];
echo $gamertag;
You need to add the full url eg:
<?php
$request_url = 'http://www.xboxgamercard.org/gamercard/test3/xbox.php';
$json = file_get_contents($request_url);
$data = json_decode($json, true);
//Example output
echo $data['gamertag']; //Crylics
echo $data['gamerscore']; //7492
/* Too access the recent_games key you will need
to loop through it or access it like
*/
echo $data['recent_games'][1]['title']; //Call of Duty: WaW
?>

Categories