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'];
?>
Related
I'm trying to return a value from my API using PHP, api can be found here:
code is as follows:
Im not seeing the echo on my page, don't see any errors and I believing im reading the json in correctly. Any help appreciated!
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue["Age"];
?>
From what I can tell, the json is not valid on the server side (due to the "connected to db" text in front of the {} part). I think it would be a good idea to fix the server side response json data, if possible!
For now, here is a way to get the value it looks like you are intending to retrieve:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$adjusted_response = str_replace('connected to db', '', $response);
$returnvalue = json_decode($adjusted_response, true);
echo $returnvalue['tv_shows']['Age'];
?>
Output:
$ php example.php
16+
If the server side json data is fixed, I think you could shorten the code to something like this:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue['tv_shows']['Age'];
?>
The thing is that $response is returned as string , in order to fix that you need to edit your backend and make it give the response without "connected to db"
I want to print result from API...
But it is not working... I don't know what is wrong I tried to do it but got this and it is not working:
<?php
$api = 'https://api.battlemetrics.com/servers/'; // the main API for servers.
$server = '5090469'; // the number of the server you will get the info from most of thime something like 5484856.
$api_full = $api . $server; // bringing them together.
$json = file_get_contents("$api_full"); // Putting the content of the file in a variable.
$response = json_decode($json, true); // decode the JSON feed
if ($response['data']['status'] == "online") {
echo "ONLINE";
} else {
echo "OFFLINE";
};
?>
Extra if you want to see the json response this is the link: https://api.battlemetrics.com/servers/5090469
Thanks in advance for your tips and help.
Looks like you are just missing a key in your if statement.
It should be:
if($response['data']['attributes']['status'] == "online")
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.
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 />";
}
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);