我正在创建一个需要最新 iPhone 应用程序价格的网站,例如在 appshopper 上,感谢 stackoverflow 上的 gbc 我被引导使用 iTunes 搜索 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>
谢谢,我非常感谢任何人可以提供的帮助。
最佳答案
这应该可以帮助您入门。
/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。
关于php - 如何使用 PHP 从 iTunes Search API JSON 页面中提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4573130/