duadlkc5762218 2011-01-01 03:52
浏览 245
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • dongqiaozhe5070 2011-01-01 06:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试