I am trying to get some data from this api:
https://www.bitstamp.net/api/ticker/
into a jquery variable (to eventually display on a web page). It returns a JSON dictionary (more information here https://www.bitstamp.net/api/).
I tried for hours on end doing it all client side but realised that I cant because the api doesn't support cross-origin requests nor JSONP. So I then moved onto serverside code:
I have a php file 'test.php' with the following code:
<?php
$homepage = file_get_contents('https://www.bitstamp.net/api/ticker/');
echo $homepage;
?>
Then inside my html page I have the following code:
<script>
var last = JSON.parse(test.php)["last"]
document.getElementById('apidata').innerHTML=last;
</script>
<span id="apidata"></span>
But I don't know why it's not working! Can anyone please shed some light on this?
I thought jquery may be simpler but if anyone knows how to get this done with JS I'd like to hear that too. I also suspect that my php file is wrong.
EDIT: Here's a link to my php file http://www.buyabitcoin.co.uk/beta/test/test.php and my html file http://www.buyabitcoin.co.uk/beta/test/test.html
username: 'test' password: 'test123'
EDIT: I have also tried
$.getJSON('test.php', function(response) {$("#apidata").html(response.value); });
in the html but to no avail. Can anyoneplease confirm if my php is outputting a JSON rather than a string?
Many thanks in advance
JSON.parse(test.php)The parse function will look for an object test and its node php. You forgot the quotation marks. If you're on jquery, give this a try:$.getJSON('test.php', function(response) { /* do this and that with the response */ });`