Logan
Logan

Reputation: 45

How do I extract values from the iTunes Search API JSON page with PHP?

I am creating a website which is going to require up-to-date iPhone App prices, such as on appshopper, and thanks to gbc here on stackoverflow I was directed towards using the iTunes Search API. I am somewhat familiar with PHP though I don't practice it that often and am not familiar with the use of JSON or how to extract values from it. I tried to use the tutorial here: http://webhole.net/2009/11/28/how-to-read-json-with-javascript/ to get it to work, though I had no luck, no data was pulled and I am not skilled enough to figure it out. I have also attempted numerous other tutorials, though the one above seemed to fit the closest to what I needed. This is going to be a necessary portion of the site, though it is not the key component of the site, so it doesn't need to be very robust, it just needs to work. I would appreciate any help, suggestions, or links.

Here is the code that I tried to use, I'm not sure if this is incapable of doing what I want it to or if I made some small error that I'm not sure of because I just followed a tutorial and don't know what I am doing.

<input type="text" id="query" /><button>search</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=';
var query;
 $('button').click(function(){
  query=$("#query").val();
  $.getJSON(url+query,function(json){
   $.each(json.results,function(i,app){
      $("#results").append('<p>'+app.trackName+'</p>');
   });
  });
 });
});
</script>

Thank you, I really appreciate any help that anyone can provide.

Upvotes: 3

Views: 8456

Answers (3)

julien_c
julien_c

Reputation: 5092

You should use JSONP. That way you don't need to write any server-side code.

Upvotes: 3

sissonb
sissonb

Reputation: 3780

This should get you started.

/wsLookup.php (Server 'proxy' to Apple API)

<?php
// the id for the Yelp app
$id = "284910350";
if (isset($_GET["id"])) {
    // Get the id from the ajax call
    $id = $_GET["id"];
}
// add the id to the url
$apiUrl = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=".$id;

// setup the cURL call
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $apiUrl);
curl_setopt($c, CURLOPT_HEADER, false);

// make the call
$content = curl_exec($c);
curl_close($c);
?>

/index.html (Client code that will access the Apple API through the 'proxy')

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var url = 'http://www.bgsisson.com/wsLookup.php';
        $('button').click(function() {
            query = $("#query").val();

            $.getJSON(url, {id:query}, function(json) {

                alert("price: " + json.results[0].price);
                alert("description: " + json.results[0].description);
                alert("artistName: " + json.results[0].artistName);

                // use html console to inspect the rest of this object
                console.log(json);
            });
        });
    });
</script>
<input type="text" id="query"/>
<button>search</button>
<br/>

<div id="results"></div>

I have this code hosted on http://www.bgsisson.com/test.html if you want to look at it. Here is the id for the Yelp app, 284910350.

Upvotes: 3

sissonb
sissonb

Reputation: 3780

The error you get with that code is,

XMLHttpRequest cannot load http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=s. Origin http://www.bgsisson.com is not allowed by Access-Control-Allow-Origin.

It says this because the origin of the call is not on the same domain, port, or protocol as the destination. more information about this here -> (http://developer.yahoo.com/javascript/howto-proxy.html)

All modern web browsers impose a security restriction on network connections, which includes calls to XMLHttpRequest. This restriction prevents a script or application from making a connection to any web server other than the one the web page originally came from (Internet Explorer will allow cross-domain requests if the option has been enabled in the preferences). If both your web application and the XML data that application uses come directly from the same server, then you do not run into this restriction.

What you'll need to do is use your server(PHP) to make the call to http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup, then pass that information to the client(JavaScript)

Where are you hosting your code? Are PHP and cURL installed? What OS are you using? I can help you more on this problem with that information.

Upvotes: 3

Related Questions