PHP - 从url获取所有json数据并将其保存到sql

时间:2017-11-12 12:54:36

标签: php json

使用此代码,我得到一个电流,并可以将其保存到sql db

$url = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=BTC-NEO";
$response = file_get_contents($url);
$obj = json_decode($response,true);

$marketname = $obj['result'][0]['MarketName'] . '';
$high= $obj['result'][0]['High'] . '';

$sql = "INSERT INTO markets (market,high) VALUES ('$marketname','$high')";
...

使用此URL我可以获得所有货币

$url = "https://bittrex.com/api/v1.1/public/getmarketsummaries";

现在问题..我想在数据库中保存所有货币。那么我如何获取所有数据到我的字符串并将所有数据保存到数据库?这不是正确的方法..

$url = "https://bittrex.com/api/v1.1/public/getmarketsummaries";
$response = file_get_contents($url);
$obj = json_decode($response,true);

$marketname = $obj['result']['MarketName'] . '';
...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

只需循环结果并将每个项目插入数据库,就像这样

...

foreach($obj['result'] as $result) {
    $marketname = $result['MarketName'];
    $high = $result['High'];

    $sql = "INSERT INTO markets (market,high) VALUES ('$marketname','$high')";

    ...
}

...