通过PHP从URL获取JSON [英] Get JSON from a URL by PHP
问题描述
我有一个返回如下JSON对象的URL:
{ "USD":{"15m":7809.0,"last":7809.0,"buy":7809.85,"sell":7808.15,"symbol":"$"}, "AUD":{"15m":10321.42,"last":10321.42,"buy":10322.54,"sell":10320.3,"symbol":"$"}, }
URL: https://blockchain.info/ticker 更多信息: https://blockchain.info/api/exchange_rates_api
我想从第一行获取所有数据并回显它,并让它不断请求它,以便其实时显示
我已经在git hub上使用了示例 https://github.com/blockchain/api-v1-client-php/blob/master/docs/rates.md 但它会显示所有数据输出,您必须刷新它才能更新
请让我指出正确的方向
理想情况下,我最终会得到类似 15 m最后买入卖出 美元($)7794.87 7794.87 7795.72 7794.02我有表和要去的数据,但它回显了整个数据集而不是第一行,而且我也不知道如何选择单个字段
如何通过PHP做到这一点?
您需要的是一个请求php页面,它将使:
1-从站点获取数据:
$data = file_get_contents('https://blockchain.info/ticker');
2-解码json
$decodedData = json_decode($data);
3-在这里您可以使用OOP来访问它:
var_dump($decodedData->USD);
这里的重点是根据需要检索数据,例如,您可以将其与HTML混合在表中.
然后,您需要一个JS脚本,该脚本将使用setInterval执行一个函数,每隔几毫秒.该函数应该向您先前创建的PHP页面发出请求,获取该数据并使用更新后的数据进行更改.
I have a URL that returns a JSON object like this:
{ "USD" : {"15m" : 7809.0, "last" : 7809.0, "buy" : 7809.85, "sell" : 7808.15, "symbol" : "$"}, "AUD" : {"15m" : 10321.42, "last" : 10321.42, "buy" : 10322.54, "sell" : 10320.3, "symbol" : "$"}, }
URL : https://blockchain.info/ticker more info : https://blockchain.info/api/exchange_rates_api
I want to get all the data from the first line and echo it and to have it keep requesting it so its live
I have used the examples on git hub https://github.com/blockchain/api-v1-client-php/blob/master/docs/rates.md but it displays all of the data output and you have to refresh it to get it updated
please can some one point me in the right direction
ideally I would end up with something like 15 m Last Buy Sell USD ($) 7794.87 7794.87 7795.72 7794.02
I have the table and data going to the table but its echoing the whole data set rather than the first line and also I dont know how to select individual fields
How can I do it through PHP?
What you need is a request php page, which will make:
1 - Get data from te site:
$data = file_get_contents('https://blockchain.info/ticker');
2 - decode the json
$decodedData = json_decode($data);
3 - Here you can access it using OOP:
var_dump($decodedData->USD);
The point here will be to retrieve data as you wish, you can mix it up with HTML in a table for example.
Then, you need a JS script, that will execute a function with setInterval, each few miliseconds. That function should make a request to a PHP page that you created earlier, get that data and change with the updated one.
这篇关于通过PHP从URL获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!