从网址中将数据插入MySQL

时间:2018-10-13 09:13:52

标签: php json xml

我有这两个URL

https://www.live-rates.com/rates?rate_format=xml

https://www.live-rates.com/rates?key=07b8395b08

并希望将其以php代码导入到mysql数据库中,并每10分钟更新一次

我尝试使用该代码,但不起作用

<?php
//
        $_user = '';
        $_password= '';
        $_db = 'test';
        $_host = 'localhost';
        $_port = 3306;
    $con = new mysqli($_host, $_user, $_password, $_db) or die(mysql_error);

$content=file_get_contents("https://www.live-rates.com/rates?key=07b8395b08");
$data=json_decode($content);

     $id = $json->device->sn;
    foreach($json->data as $key => $data){
        if(empty($data) || !isset($data->{'$ts'})){
            continue;
        }
        if (isset($data->{'$msg'})){
            $msg = $data->{'$msg'};
            $time = $data->{'$ts'};

            $sql="INSERT into error_log ( currency , rate  , Bid , Ask , high  , low  , open  , close  , timestamp ) VALUES (?,?,?,?,?,?,?,?,?); ";
            $stmt = $con-> prepare($sql);
            $stmt -> bind_param("iss", $id,$time, $msg);
            $stmt -> execute();
        }else{
            $time = (isset($data->{'$ts'}))? $data->{'$ts'}:'';
            $RH = (isset($data->RH))? $data->RH:'';
            $AT = (isset($data->AT))? $data->AT:'';
            $MINVi = (isset($data->MINVi))? $data->MINVi:'';

            //insert into mysql table
            $sql="INSERT into tx (currency , rate  , Bid , Ask , high  , low  , open  , close  , timestamp) VALUES (?,?,?,?,?,?,?,?,?); ";
            $stmt = $con-> prepare($sql);
            $stmt -> bind_param("issss", $id,$time,$RH,$AT,$MINVi);
            $stmt -> execute();
        }


    }
    mysqli_close($con);

?>

总是向我显示此消息

Notice: Undefined variable: json in xxxxxx\add.php on line 23

Notice: Trying to get property of non-object in xxxxxx\add.php on line 23

Notice: Trying to get property of non-object in xxxxxxx\add.php on line 23

Notice: Undefined variable: json in xxxxxxxxx\add.php on line 24

Notice: Trying to get property of non-object in xxxxxxxx\add.php on line 24

Warning: Invalid argument supplied for foreach() in xxxxxxxx\add.php on line 24

1 个答案:

答案 0 :(得分:0)

GET和POST取决于您传递的内容 提取($ _GET); 将插入查询与键和值一起使用