im new in PHP developing and need your help.
I write a webpage summoner-info.com with the RIOT API.
But im to bad to understand the documentation.
I wana output via echo my states. In the API docs stands:
Return Value: Map[string, List[LeagueDto]]
But i dont understand how to use this.
Doc link: link
I wrote this
$url = "https://{$region}.api.pvp.net/api/lol/{$region}/v2.5/league/by-summoner/{$summoner_ID}?api_key={$api}";
$data = file_get_contents($url);
$data = json_decode($data, true);
print_r($data);
So how can i write something like this
echo $data["tier"["LeagueDto "]]
Assuming this is the kind of response you're expecting (2 summoner ids):
https://github.com/josephyi/taric/blob/master/spec/fixtures/leagues_by_summoner_ids.json
There's no LeagueDto entry in the JSON response. When Riot refers to 'LeagueDto' that's the class that represents the data of the object, but is not meant to be accessed from the response. If you look at the response, you'll have to navigate the JSON. I don't know PHP, but assuming you want summoner id 21066:
$data["21066"] // array of leagues the summoner is in
$data["21066"][0] // first league the summoner is in
$data["21066"][0]["entries"] // array of league entries for the first league
$data["21066"][0]["tier"] // tier of first league
Hope that helps!
Related
I am trying to use a weather API for a project and I am trying to build a simple weather app that will check the forecast and display it. The code looks like this:
$url = "http://api.openweathermap.org/data/2.5/weather?zip=30553&appid={APIKEY}";
$data = file_get_contents($url);
$decodeJSON = json_decode($data);
$testvar = "weather[0]->main";
echo $decodeJSON->$testvar;
and ideally I would like to be able to change $testvar to point at different variables returned by the API and as of right now I know it can't be done by combining "$decodeJSON->" and "$testvar", but is there anyway to achieve something close to what I have above that basically just assembles the two?
Appreciate all the help in advance and any feedback on the code is greatly appreciated!
I'm using PHP to build an application that gets some statistics from individual Instagram accounts.
I do a get request to the following URL to get a JSON formatted string:
https://www.instagram.com/{username}/?__a=1
With json_decode, I get the followers and the followings by:
$follows = $data['user']['follows']['count'];
$followedBy = $data['user']['followed_by']['count'];
But I don't know how to get the posts count.
For example for NASA profile:
https://www.instagram.com/nasa/
... I need the number (currently) 1.967.
Any idea?
And a final question: do I have to use API Key to get the stats, or the GET to the above URL is enough?
what is wrong with
$json = file_get_contents('https://www.instagram.com/nasa/?__a=1');
$obj = json_decode($json);
echo $obj->user->media->count;
it just spit out the correct post count as you wanted?
As of now (2020) Instagram has made some changes to the JSON response and the code below does the same as the accepted answer above, also with formatted output.
$ig = json_decode(file_get_contents('https://www.instagram.com/nasa/?__a=1'));
echo number_format($ig->graphql->user->edge_owner_to_timeline_media->count);
I have been building a test app that gathers pictures and certain pieces of user information from Instagram (only using PHP). I am not using the official API, but instead parsing the JSON response that I get from the url.
I'm trying to gather comments for a certain post. The issue is that when there's more than one comment, I can see the text for each comment, but the object name in the response is the same (it repeats for every comment): {text}.
Here's an abbreviated example response:
{"text":"I think this is funded by my company","created_at"...,"user":{"username"...}, blah blah blah},
{"text":"Very cool","created_at"...,"user":{"username"...blah blah blah
As you can see, for each comment made, there is a "text" object that I need to grab.
Here is my function (shortened) that parses the JSON and gets the comment text:
function scrape_insta_user_post($postid) {
$insta_source = file_get_contents('https://www.instagram.com/p/'.$postid.'/');
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
global $the_pic_comments;
$the_pic_comments = $insta_array['entry_data']['PostPage'][0]['media']['comments']['nodes'][0]['text'];
}
I have another function that I use to display $the_pic_comments by simply echoing the results.
echo $the_pic_comments;
In cases where there is more than one {text} object, how would I go about displaying each comment? I'm assuming some kind of foreach() loop might work, but I can't get the foreach() to work with my json_decode() function.
This currently works, but only displays one comment, and everything else is ignored.
Can you help me create a loop that will get each comment from the JSON response?
Thanks!
Judging from your code alone, it seems that it should be:
foreach ($insta_array['entry_data']['PostPage'][0]['media']['comments']['nodes'] as $comment) {
echo $comment['text']; // Or add to array, eg. $comments[] = $comment['text'];
}
Note that since you are not using official API, your mechanism may break any time without notice shall Instagram change anything in their code.
Im new to php and json and usage of API's. Im on a big data project and I want to create a web application that retrieves information from the API in order to create predictive analysis.
I tried to use this code to retrieve data but all I see is a blank page. http://20ff.net/index.php
<html>
<head>
<title>RIOT API SBOX</title>
</head>
<body>
<?php
$json = json_decode(file_get_contents('https://euw.api.pvp.net/api/lol/euw/v2.2/matchhistory/31827832?rankedQueues=RANKED_SOLO_5x5&api_key=key'), true);
var_dump(json_decode($json));
echo $json[0]['firstBloodKill'];
?>
</body>
</html>
Yes, I removed the api key myself, with key its not working also.
Please help me out, are there any basic tutorials how to return information in JSON between php tags?
For reasons that aren't clear to me, it looks like the rankedSoloGamesPlayed field isn't returned for all champions. In fact, in your query it's only returned for a single champion (index 21):
<?php
$KEY = "<Your API Key>";
$url=sprintf('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/31827832/ranked?season=SEASON4&api_key=%s', $KEY);
$data=file_get_contents($url);
$json=json_decode($data);
// Uncomment this if you want to see the full decoded JSON
//print_r($json);
echo $json->champions[21]->stats->rankedSoloGamesPlayed;
// This displays 0, which is the value returned in the JSON
// Additionally, you could access the totalDamageDealt field of the champion with index 0 with:
echo $json->champions[0]->stats->totalDamageDealt;
// This displays 214660
Note how you have to traverse the decoded JSON. If print_r says it's an Array, you can traverse it with brackets ([21]), but if it says it's a stdClass Object, you traverse it with the arrow notation.
I'm trying to get a user's site_id from posterous. This data seems to be nowhere in the user's profile, and nothing in the FAQ or help or docs indicates how to find this.
The only method I can find is to request a list of sites over API,
So, that's what I'm trying to do, and I get back a json response, and, indeed, the site_id of any site the user has on posterous is in there, and I just want to output this.
First I tried with (where $result is the json response)
$siteid = preg_match('"site_id": \d+', $result);
echo $siteid;
I get nothing.
Then I tried with
json_decode($result);
echo $result->site_id;
I still get nothing.
I can see, in the json response ($result),
"site_id": $somenumber;
Why can't I extract this data with either preg_match or json_decode? Is there another, better method?
My full code is pasted at http://tonybaldwin.me/paste/index.php?3u
The Posterous list-of-sites API returns a list (as you might expect) of mappings. Thus, what you need to do is first pick the right item of the list, and then read it's id key.
$result = json_decode($response);
$first_result = $result[0];
$first_result_id = $first_result["id"];
echo $first_result_id;