I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?
-
1Look up "JSON parser" and start from there. Any refined, specific questions please feel free to ask again. This question as it stands is susceptible to downvotes.– TEKCommented Aug 30, 2014 at 12:09
-
try using jquery and parsing the json data– undefined_variableCommented Aug 30, 2014 at 12:11
3 Answers
First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true
means that $remote
should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.
-
I get: Catchable fatal error: Object of class stdClass could not be converted to string in (my PHP file) on line 17--line 17 is $data = json_decode($remote, true);– meecoderCommented Aug 30, 2014 at 12:24
Use json_decode
to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?
You can either do it with javascript or php.
With javascript use this: http://api.jquery.com/jquery.getjson/ You take the pages output and push them as variables to the php.
With php you can use http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.