Jump to content

dealing with APIs


Marco Angeli
 Share

Recommended Posts

Thanks Diogo,

Ryan's module is perfect to transform a PW site into an API.

But I want to do something different:

I'd like to read (parse) an external API like this one:

{
 "response": {
 "version": "0.1",
 "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
 "features": {
 "conditions": 1
 }
 },
 "current_observation": {
 "image": {
 "url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
 "title": "Weather Underground",
 "link": "http://www.wunderground.com"
 },
 "display_location": {
 "full": "San Francisco, CA",
 "city": "San Francisco",
 "state": "CA",
 "state_name": "California",
 "country": "US",
 "country_iso3166": "US",
 "zip": "94101",
 "latitude": "37.77500916",
 "longitude": "-122.41825867",
 "elevation": "47.00000000"
 },
 "observation_location": {
 "full": "SOMA - Near Van Ness, San Francisco, California",
 "city": "SOMA - Near Van Ness, San Francisco",
 "state": "California",
 "country": "US",
 "country_iso3166": "US",
 "latitude": "37.773285",
 "longitude": "-122.417725",
 "elevation": "49 ft"
 },
 "estimated": {},
 "station_id": "KCASANFR58",
 "observation_time": "Last Updated on June 27, 5:27 PM PDT",
 "observation_time_rfc822": "Wed, 27 Jun 2012 17:27:13 -0700",
 "observation_epoch": "1340843233",
 "local_time_rfc822": "Wed, 27 Jun 2012 17:27:14 -0700",
 "local_epoch": "1340843234",
 "local_tz_short": "PDT",
 "local_tz_long": "America/Los_Angeles",
 "local_tz_offset": "-0700",
 "weather": "Partly Cloudy",
 "temperature_string": "66.3 F (19.1 C)",
 "temp_f": 66.3,
 "temp_c": 19.1,
 "relative_humidity": "65%",
 "wind_string": "From the NNW at 22.0 MPH Gusting to 28.0 MPH",
 "wind_dir": "NNW",
 "wind_degrees": 346,
 "wind_mph": 22.0,
 "wind_gust_mph": "28.0",
 "wind_kph": 35.4,
 "wind_gust_kph": "45.1",
 "pressure_mb": "1013",
 "pressure_in": "29.93",
 "pressure_trend": "+",
 "dewpoint_string": "54 F (12 C)",
 "dewpoint_f": 54,
 "dewpoint_c": 12,
 "heat_index_string": "NA",
 "heat_index_f": "NA",
 "heat_index_c": "NA",
 "windchill_string": "NA",
 "windchill_f": "NA",
 "windchill_c": "NA",
 "feelslike_string": "66.3 F (19.1 C)",
 "feelslike_f": "66.3",
 "feelslike_c": "19.1",
 "visibility_mi": "10.0",
 "visibility_km": "16.1",
 "solarradiation": "",
 "UV": "5",
 "precip_1hr_string": "0.00 in ( 0 mm)",
 "precip_1hr_in": "0.00",
 "precip_1hr_metric": " 0",
 "precip_today_string": "0.00 in (0 mm)",
 "precip_today_in": "0.00",
 "precip_today_metric": "0",
 "icon": "partlycloudy",
 "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
 "forecast_url": "http://www.wunderground.com/US/CA/San_Francisco.html",
 "history_url": "http://www.wunderground.com/history/airport/KCASANFR58/2012/6/27/DailyHistory.html",
 "ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.773285,-122.417725"
 }
}
 

and print in human readable language some results on a web page.

Is there an easy way of doing this?

Link to comment
Share on other sites

@Marco, thanks for posting this. I just used it to create a feed from a site that wanted to charge my client $50/month to have access to their api;

now we can have an api feed from that site using kimono for free. So far it seems to be working really well; it's really quite an amazing app;

right now i have gotten as far as reading the json into my processwire site, but i do also need to now figure a way to take this json string and make it into my markup for the output... so i guess i'm a bit stuck in the same place as you are, but if i come up with anything i'll report back.

  • Like 1
Link to comment
Share on other sites

right now i have gotten as far as reading the json into my processwire site, but i do also need to now figure a way to take this json string and make it into my markup for the output... so i guess i'm a bit stuck in the same place as you are, but if i come up with anything i'll report back.

Soma's right you guys, you just need to use json_decode to get it into an array. From there you can foreach through the array and do whatever you want with it. You could even extract the content to add to PW pages for storage if you want.

Given kimonolabs limits on the free option, that might be a decent idea. Cache the results in PW. They have a limit of 100 per hour, so even once a minute would be ok.

  • Like 1
Link to comment
Share on other sites

@adrian, thanks, yes, already got the json decoded and into arrays/variables; so it's all good;

i can post examples here; I wouldn't know how to cache the results though in PW... that would be cool...!

i think my client wants to have the possibility of changing stuff often on the feed server, so maybe caching is not a good idea?

Link to comment
Share on other sites

Marco - as Soma says, use json_decode to turn the JSON you posted into an array, then just use ProcessWire's API as normal to create pages and add this content to your pages if that's what you want to do: http://processwire.com/talk/topic/352-creating-pages-via-api/

If you just want to simply output that data and not save it, then as Soma says, just use json_decode and it will be a normal PHP array that you can use.

I think you're thinking there's more to it than that, but there's really not :)

  • Like 1
Link to comment
Share on other sites

Note that by default, the function json_decode() returns an object. If you try to foreach that you will get an error like "Fatal error: Cannot use object of type stdClass as array in...". So, you need to use the 2nd parameter of the function to return an array..e.g.

$array = json_decode($jsonstring, true) 

http://php.net/manual/en/function.json-decode.php

Having said that, your json string above results in a deeply nested array (multi-dimensional) which can be a pain to traverse....unless you know about stuff like these

Even Better...

http://php.net/manual/en/spl.iterators.php

http://stackoverflow.com/questions/5524227/php-foreach-with-arrays-within-arrays/5524267

http://www.phpro.org/tutorials/Introduction-to-SPL.html

http://www.sitepoint.com/using-spl-iterators-1/

http://codepad.org/A68lcnLZ [demo]

Edited by kongondo
  • Like 3
Link to comment
Share on other sites

<?php
$results = json_decode($response, TRUE);

$events = ($results["results"]);
$events = $events["collection1"];

foreach ($events as $event) {
    echo $event["title"]["text"];
    echo "\n";
    echo $event["title"]["href"];
    echo "\n";
    
    echo $event["detailslink"]["href"];
    echo "\n";
    echo $event["detailslink"]["src"];
    echo "\n";
    
    echo $event["image"]["src"];
    echo "\n";

}

?>

this is working, but i will look into the suggestions posted above... looks interesting.. don't try kimono because you'll want to kimonify every site after you try it.

Link to comment
Share on other sites

In some cases Kimono might be overkill. If all you want is to extract some simple info from a webpage you can use an html parser. Here's an example using http://simplehtmldom.sourceforge.net/

<?php

include "simple_html_dom.php";

$html = file_get_html('http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=porto,+portugal');

$location = $html->getElementById('#locationName')->plaintext;
$temperature = $html->getElementById('#rapidtemp')->find('.b')[0]->plaintext;
 
echo "Temperature in {$location} is {$temperature} °C";
 
//echoes "Temperature in Porto, Portugal is 22 °C" (that should make most of you jealous )

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...