Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Format and Display JSON from a URL

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1221 Views
  • Pix
    Avatar
    Community Member
    120 Posts

    Format and Display JSON from a URL Link to this post

    Hi All,

    I would like to format and display the results for an app from the iTunes search API as a page on my site. I am sure many of you veterans are now saying, oh how simple, but I myself am not sure where to begin. Here is an example search:

    http://itunes.apple.com/lookup?id=422876559

    As you can see, you get JSON formatted data with all kinds of useful information including links to artwork which I would like to display as images on my page. It's pretty cool, the API search documentation is here:
    http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html

    So basically, how do I retrieve the JSON info from the URL and use the data provided in SS? I am using 2.4.5.

    Thanks for any pointers!!!

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Format and Display JSON from a URL Link to this post

    You have a couple options - do you want to load the JSON through PHP (which can cache) or use javascript on the front end, usually you'll want to do this in PHP. RestfulService is the class you want for fetching API's, sadly it doesn't yet support JSON natively and the docs are out of date for 3.0 but you can include the following (the shortest possible) in any PHP code where you want to load the feed..

    $service = new RestfulService("http://itunes.apple.com/");
    $response = $service->request("lookup?id=422876559");
    print_r(json_decode($response->getBody()));

    The important line is json_decode($response->getBody()); This converts the JSON to an array when you can then use to pass data back to the template or doing other things with.

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Format and Display JSON from a URL Link to this post

    Oh wow! That's cool! I am going to try this out thank you so much!

    p.s. I had read about caching on the Apple website to increase response time (and of course not overload their servers), but I did not even want to address it in this question....figured I would in the least start out trying to get data I could use. Turns out you answered it anyway! Thanks for this informative and thorough response.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Format and Display JSON from a URL Link to this post

    See http://api.silverstripe.org/2.4/sapphire/integration/RestfulService.html for all the options you can configure but RestFulService out of the box will cache your data for an hour. You can customize this period in the contstructor.

    1221 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.