简体   繁体   English

从PHP脚本中获取变量

[英]Take variables from a PHP script

I am making a website to display the data at https://api.captcoin.com/address/top/100 . 我正在建立一个网站,以显示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. 然后,由于数据是json格式,您需要使用json_decode函数对其进行解码。

$data = json_decode($remote, true); 

true means that $remote should be decoded as associative array. true表示$remote应解码为关联数组。

And finally you can access the data like an ordinary php array: 最后你可以像普通的php数组一样访问数据:

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. 使用json_decode将该url的内容转换为数组,然后像通过任何数组一样搜索它。

To get the actual content of the site please refer to this post Get file content from a URL? 要获取网站的实际内容,请参阅此帖子从URL获取文件内容?

You can either do it with javascript or php. 你可以用javascript或php来做。

With javascript use this: http://api.jquery.com/jquery.getjson/ You take the pages output and push them as variables to the php. 用javascript使用这个: http//api.jquery.com/jquery.getjson/你把页面输出并将它们作为变量推送到php。

With php you can use http://php.net/manual/en/function.json-decode.php 使用php,您可以使用http://php.net/manual/en/function.json-decode.php

You make an array to push the json data into: 您创建一个数组以将json数据推送到:

$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. 请注意,这是未经测试的,请阅读json_decode api以自定义函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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