使用php解析url
问题描述:
I am having problem parsing this url using php language. I don't know if it is JSON or a normal API. I need to get "ask" and "bid" from each one. Thank you so much.
http://www.fxgrow.com/quotes/quotes.php
it's structure is like this:
{
EURUSD: {
dir: 1,
ask: "1.13729",
bid: "1.13724"
},
USDJPY: {
dir: 1,
ask: "119.365",
bid: "119.359"
},
USDCHF: {
dir: 1,
ask: "0.94013",
bid: "0.93994"
},
GBPUSD: {
dir: 1,
ask: "1.54218",
bid: "1.54209"
},
AUDUSD: {
dir: 1,
ask: "0.77979",
bid: "0.77973"
},
NZDUSD: {
dir: 1,
ask: "0.75103",
bid: "0.75092"
},
GBPJPY: {
dir: 1,
ask: "184.081",
bid: "184.064"
},
EURGBP: {
dir: 1,
ask: "0.73749",
bid: "0.73743"
}
我在使用php语言解析此url时遇到问题。 我不知道它是JSON还是普通的API。 我需要从每个人那里得到“询问”和“出价”。 非常感谢你。 p>
http://www.fxgrow .com / quotes / quotes.php p>
它的结构如下: p>
{
EURURD:{
dir:1,
问:“1.13729”,
bid:“1.13724”
},
NDDJPY:{
dir:1,
ask:“119.365”,
bid:“119.359”
},
USDCHF:{
dir:1,
ask:“0.94013”,
bid:“0.93994”
},
GBPUSD:{
dir:1,
ask:“1.54218”,
n bid:“1.54209”
},
AUDUSD:{
dir:1,
ask:“0.77979”,
bid:“0.77973”
},
NZDUSD:{
dir:1,\ n问:“0.75103”,
bid:“0.75092”
},
GBPJPY:{
dir:1,
问:“184.081”,
bid:“184.064”
},
EURLP: {
dir:1,
ask:“0.73749”,
bid:“0.73743”
}
code> pre>
div>
答
$json = file_get_contents('http://www.fxgrow.com/quotes/quotes.php');
$data = json_decode($json);
foreach ($data as $key => $value) {
echo $key . ": ask = " .$value->ask ." | bid =".$value->bid . " <br />";
}
答
Just use json_decode. You can then access the data as an object eg.
$data = file_get_contents('http://www.fxgrow.com/quotes/quotes.php');
$decoded = json_decode($data);
print_r($decoded);
print '<p>EURUSD ask: ' . $decoded->EURUSD->ask . '</p>';
答
it is pure json formatted data so you can parse it using built in functions.
$data=' { EURUSD: { dir: 1, ask: "1.13729", bid: "1.13724" }, USDJPY: { dir: 1, ask: "119.365", bid: "119.359" }, USDCHF: { dir: 1, ask: "0.94013", bid: "0.93994" }, GBPUSD: { dir: 1, ask: "1.54218", bid: "1.54209" }, AUDUSD: { dir: 1, ask: "0.77979", bid: "0.77973" }, NZDUSD: { dir: 1, ask: "0.75103", bid: "0.75092" }, GBPJPY: { dir: 1, ask: "184.081", bid: "184.064" }, EURGBP: { dir: 1, ask: "0.73749", bid: "0.73743" }';
$json=(object)json_decode( $data, true );
foreach( $json as $key => $value ){
$o=(object)$value;
echo $key.' '.$o->dir.' '.$o->ask.' '.$o->bid.'<br />';
}
答
Try this..
<?php
$test='{"EURUSD":{"dir":1,"ask":"1.14498","bid":"1.14492"},"USDJPY":{"dir":1,"ask":"118.729","bid":"118.724"},"USDCHF":{"dir":1,"ask":"0.93974","bid":"0.93944"},"GBPUSD":{"dir":1,"ask":"1.54587","bid":"1.54579"},"AUDUSD":{"dir":1,"ask":"0.77758","bid":"0.77754"},"NZDUSD":{"dir":1,"ask":"0.75293","bid":"0.75283"},"GBPJPY":{"dir":0,"ask":"183.540","bid":"183.525"},"EURGBP":{"dir":1,"ask":"0.74076","bid":"0.74063"}}';
$data=json_decode($test ,true);
foreach($data as $value)
{
$array=array();
$array[]=$value;
foreach($array as $val)
{
echo $val['ask']."</br>";
echo $val['bid']."</br>";
}
}