简体   繁体   中英

Take variables from a PHP script

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM