如何使用PHP从iTunes Search API JSON页面中提取值?

时间:2011-01-01 03:52:34

标签: php json itunes

我正在创建一个需要最新iPhone App价格的网站,例如在appshopper上,并且由于 gbc 在stackoverflow上,我被指向使用iTunes Search API 。我对PHP有点熟悉,虽然我不经常练习它,并且不熟悉JSON的使用或如何从中提取值。我尝试使用这里的教程:http://webhole.net/2009/11/28/how-to-read-json-with-javascript/让它工作,虽然我没有运气,没有数据被拉,我不够熟练,无法弄明白。我也尝试了很多其他的教程,虽然上面的教程似乎最接近我的需要。这将是网站的必要部分,虽然它不是网站的关键组成部分,因此它不需要非常强大,只需要工作。我将不胜感激任何帮助,建议或链接。

以下是我尝试使用的代码,我不确定这是否无法执行我想要的操作,或者我做了一些我不确定的小错误,因为我只是按照教程和不知道我在做什么。

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

谢谢,非常感谢任何人都可以提供的任何帮助。

3 个答案:

答案 0 :(得分:3)

您使用该代码获得的错误是

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.

它说这是因为呼叫的来源与目的地不在同一个域,端口或协议上。有关这方面的更多信息 - &gt; (http://developer.yahoo.com/javascript/howto-proxy.html

  

所有现代Web浏览器都对网络连接施加了安全限制,包括对XMLHttpRequest的调用。此限制可防止脚本或应用程序连接到除最初来自网页之外的任何Web服务器(如果在首选项中启用了该选项,则Internet Explorer将允许跨域请求)。如果您的Web应用程序和应用程序使用的XML数据都直接来自同一服务器,那么您不会遇到此限制。

您需要做的是使用您的服务器(PHP)来调用http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup,然后将该信息传递给客户端(JavaScript)

您在哪里托管代码?是否安装了PHP和cURL?你用的是什么操作系统?我可以用这些信息帮助你解决这个问题。

答案 1 :(得分:3)

这应该让你开始。

/wsLookup.php(服务器'代理'到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(将通过'代理'访问Apple API的客户端代码)

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

如果您想查看此代码,我会在http://www.bgsisson.com/test.html上托管此代码。这是Yelp应用程序的ID,284910350。

答案 2 :(得分:3)

您应该使用JSONP。这样您就不需要编写任何服务器端代码了。