So I am trying to pull data from a website api, they didn't have any documentation for what I am trying to do..
So what I am trying to pull is the 4JNET Price. "price":"0.000000011816"
What Iv'e tried:
<?php
//4JNET
$content = file_get_contents('https://www.lbank.site/request/tick?
symbol=alts');
preg_match('#{"symbol":"4jnet/usdt"(.*)"usd"#', $content, $match);
$Price = $match[1];
echo $Price;
?>
Here is some sample data...
{"symbol":"4jnet/usdt","vol":"5078919159375.1975","amount":"58729.3647","c":{"price":"0.000000011816","usd":"0.00","cny":"0.00"},"toCny":"6.36","change":"-5.98","h":{"price":"0.000000015042","usd":"0.00","cny":"0.00"},"l":{"price":"0.000000010099","usd":"0.00","cny":"0.00"},"dir":"buy","toUsd":"1.00","platform":"LBANK","timestamp":1639627875492},{"symbol":"doe/usdt","vol":"6164668.8295","amount":"323916.8607","c":{"price":"0.0606","usd":"0.06060","cny":"0.38589"},"toCny":"6.36790","change":"33.48","h":{"price":"0.0625","usd":"0.06250","cny":"0.39799"},"l":{"price":"0.0433","usd":"0.04330","cny":"0.27573"},"dir":"buy","toUsd":"1.00000","platform":"LBANK","timestamp":1639627874171},{"symbol":"dydx/usdt","vol":"819823.0898","amount":"6853463.1339","c":{"price":"8.4729","usd":"8.47290","cny":"53.95457"},"toCny":"6.36790","change":"4.33","h":{"price":"8.8255","usd":"8.82550","cny":"56.19990"},"l":{"price":"7.7505","usd":"7.75050","cny":"49.35440"},"dir":"sell","toUsd":"1.00000","platform":"LBANK","timestamp":1639627873783},{"symbol":"lid/eth","vol":"0.0000","amount":"0.0000","c":{"price":"0.00889996","usd":"35.74499","cny":"227.62057"},"toCny":"25575.46044","change":"0.00","h":{"price":"0.00889996","usd":"35.74499","cny":"227.62057"},"l":{"price":"0.00889996","usd":"35.74499","cny":"227.62057"},"dir":"buy","toUsd":"4016.31000","platform":"LBANK","timestamp":1639627875679},{"symbol":"lunapad/usdt","vol":"16117157.1174","amount":"453319.2567","c":{"price":"0.0362","usd":"0.03620","cny":"0.23051"},"toCny":"6.36790","change":"46.56","h":{"price":"0.0390","usd":"0.03900","cny":"0.24834"},"l":{"price":"0.0219","usd":"0.02190","cny":"0.13945"},"dir":"sell","toUsd":"1.00000","platform":"LBANK","timestamp":1639627874066},{"symbol":"tlm/usdt","vol":"4631316.5073","amount":"1003428.9213","c":{"price":"0.2237","usd":"0.22370","cny":"1.42449"},"toCny":"6.36790","change":"4.78","h":{"price":"0.2296","usd":"0.22960","cny":"1.46206"},"l":{"price":"0.2006","usd":"0.20060","cny":"1.27740"},"dir":"buy","toUsd":"1.00000","platform":"LBANK","timestamp":1639627875510},
CodePudding user response:
Try this code:
//4JNET
$content = file_get_contents('https://www.lbank.site/request/tick?symbol=alts');
// convert the received json to object in this case
$jsontoobject = json_decode($content);
$result = null;
// walk the array [data] inside the jsontoobject
foreach ($jsontoobject->data as $num => $object) {
// if matches the desired symbol
if ($object->symbol == '4jnet/usdt') {
// asign it to a var
$result = $object;
// finish the walk
break;
}
}
// show what that symbol object contains
print_r($result);
It will output the matched object like this:
stdClass Object
(
[symbol] => 4jnet/usdt
[vol] => 5124704593591.1719
[amount] => 59424.1760
[c] => stdClass Object
(
[price] => 0.000000012819
[usd] => 0.00
[cny] => 0.00
)
[toCny] => 6.36
[change] => 9.53
[h] => stdClass Object
(
[price] => 0.000000015042
[usd] => 0.00
[cny] => 0.00
)
[l] => stdClass Object
(
[price] => 0.000000010099
[usd] => 0.00
[cny] => 0.00
)
[dir] => buy
[toUsd] => 1.00
[platform] => LBANK
[timestamp] => 1639634136197
)
And then you can access to any value like this:
// access to specific value in the object
echo $result->c->price;
Output:
0.000000012088