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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Format and Display JSON from a URL


Go to End


4 Posts   2997 Views

Avatar
Pix

Community Member, 158 Posts

3 July 2012 at 8:52am

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

Avatar
Willr

Forum Moderator, 5523 Posts

3 July 2012 at 9:49pm

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.

Avatar
Pix

Community Member, 158 Posts

4 July 2012 at 11:30am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

5 July 2012 at 7:05pm

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.