Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Format and Display JSON from a URL


4 Posts   1529 Views


3 July 2012 at 8:52am Community Member, 145 Posts

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:

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:

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!!!


3 July 2012 at 9:49pm Forum Moderator, 5511 Posts

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("");
$response = $service->request("lookup?id=422876559");

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.


4 July 2012 at 11:30am Community Member, 145 Posts

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.


5 July 2012 at 7:05pm Forum Moderator, 5511 Posts

See 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.