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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!