PHP Decoding JSON and Accessing Array Value based on dynamic variable - php

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!

Related

Pulling title from API

Alright so I started with something simple just to get me familiar with what exactly I am getting my self into, how ever when tinkering I became lost.
Okay so I am trying to get contents from the following
$details = json_decode(file_get_contents("https://beam.pro/api/v1/users/63662"));
what's the best way I can go about doing this?
Currently I can display the username portion using print $details->username; and the id portion using print $details->id; but after this I become lost how could I go about pulling the title for example.
Here is what the Twitter looks like currently in the API
"name":"Thursday -- BR 2's [NA] w/ beam.pro/para",
Documentation is here
You would use the following:
echo $details->channel->name;
However, if you're more comfortable with arrays, you could do this:
$details = json_decode(file_get_contents("https://beam.pro/api/v1/users/63662"), true);
echo $details['channel']['name'];
Here is the object structure for future reference:

How to echo PHP HashMaps

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!

Parse iTunes API (which uses JSON) w/ PHP

This is the first time I have came in contact with JSON, and I literally have no idea how to parse it with PHP. I know that functions to decode JSON exist in PHP, but I am unsure how to retrieve specific values defined in the JSON. Here's the JSON for my app:
http://itunes.apple.com/search?term=enoda&entity=software
I require a few values to be retrieved, including the App Icon (artworkUrl100), Price (price) and Version (version).
The things I am having issues with is putting the URL of the App Icon into an actual HTML image tag, and simply retrieving the values defined in the JSON for the Price and Version.
Any help/solutions to this would be fantastic.
Thanks,
Jack
Yeah, i have something similar, for my App review website, here is a bit code:
$context = stream_context_create(array('http' => array('header'=>'Connection: close')));
$content = file_get_contents("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsLookup?id=$appid&country=de");
$content = json_decode($content);
$array = $content->results["0"];
$version = $array->version;
$artistname = $array->artistName;
$artistid = $array->artistId;
Thats what I used to get Information from the AppStore, maybe you can change the link and some names and it would work for you.

Scraping content from a file

I have a file that contains many of these
<sync start="14400">
<p class="ENCC">
Removed
</p>
</sync>
and I would like to turn them into this format
<p begin="00:00:33.3" end="00:00:35.8">Removed</p>
I would like to get the data inside start="" and the data inside and loop through until I have all of them on the page.
I have been trying to do this for a few hours now but could do with a point in the right direction. Any help or guidance would be greatly appreciated. Thank you
Edit: also please ignore the start/behin formatting I already have the code to do that
If what you're after is a simple way to parse XML, look into phpQuery (very accessible if you're used to jQuery). The code would look something like (untested):
$start_values = array ();
$content_values = array ();
$doc = phpQuery::newDocumentXML ($xml);
foreach (pq ('sync') as $node)
{
$start_values[] = pq ($node)->attr ('start');
$content_values[] = pq ($node)->find ('p')->html ();
}
$start_values would then be an array with the respective values for the start-attribute and $content_values would be an array with the respective content of the actual tag.
UPDATED
I noticed that I didn't take the p-node under sync into consideration earlier. The find ('p') part should take care of that.

How do I make a script to grab certain piece of data from a JSON API?

I'm trying to build a little script that would let me do this:
http://example.com/appicons.php?id=284417350
and then display this in plain text
http://a3.mzstatic.com/us/r1000/005/Purple/2c/a0/b7/mzl.msucaqmg.png
This is the API query to get that information (artworkUrl512):
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=284417350
Any help and example code would be much appreciated!
I am not sure why you have jQuery in your tags, unless you want to make the request dynamically without a page refresh. However you can do this simply in PHP using the following example:
$request = array (
"app_id" => #$_GET["id"]
);
// parse the requests.
if (empty($request["app_id"])) {
// redirects back / displays error
}
else {
$app_uri = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $request["app_id"];
$data = file_get_contents ($app_uri);
$json = json_decode (trim($data));
print($json->results[0]->artworkUrl100);
}
$request = file_get_contents($itms_url);
$json = json_decode(trim($request));
echo $json[0]->artworkUrl512;
should work in PHP. Unless of course there is more than one hit to the search. A solution using jQuery is probably not very much more difficult.

Categories