How to extract values ​​from a JSON Search Search API page using PHP?

I am creating a website that will require modern prices on the iPhone, for example, on appshopper, and thanks to gbc here in stackoverflow, I was aimed at using the iTunes search API, I am a little familiar with PHP, although I often do not do it, and I not familiar with using JSON or how to extract values ​​from it. I tried using the tutorial here: http://webhole.net/2009/11/28/how-to-read-json-with-javascript/to make it work, although I was not lucky, there was no data, and I am not experienced enough to understand this. I also tried to complete many other lessons, although one of them seemed to be the closest to what I needed. This will be a necessary part of the site, although this is not a key component of the site, so it does not have to be very reliable, it just has to work. I would appreciate any help, suggestions or links.

Here is the code that I tried to use, I'm not sure if it is unable to do what I want, or if I made a small mistake that I'm not sure about, because I just followed the 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
source share
3 answers

.

/wsLookup.php( - API Apple)

<?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( , API Apple "" )

<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, 284910350.

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

, , . β†’ (http://developer.yahoo.com/javascript/howto-proxy.html)

- , XMLHttpRequest. script -, , - (Internet Explorer , ). -, XML, , , .

, (PHP) http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup, (JavaScript)

? PHP cURL? ? .

+3

You must use JSONP . This way you do not need to write server code.

+3
source

Source: https://habr.com/ru/post/1782977/


All Articles