【发布时间】:2018-10-13 09:13:52
【问题描述】:
我有这两个网址
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
【问题讨论】:
-
欢迎您! Stack Overflow不是代码编写服务。我们总是乐于帮助和支持新的编码员,但您首先需要帮助自己。您应该尝试自己编写代码。请阅读How to create a Minimal, Complete, and Verifiable example。
-
您好,请阅读stackoverflow.com/help/how-to-ask,然后您会发现您的问题不符合stackoverflows 标准。您没有描述具体问题,但似乎要求有人免费为您的问题编写代码。
-
欢迎,为了改善您的 SO 体验,请阅读如何询问 On Topic question、Question Check list 和 the perfect question 以及如何创建 Minimal, Complete and Verifiable Example 和 TAKE THE TOUR
-
对不起,我忘记在帖子中插入代码
-
"但不起作用" 这是什么意思?它会插入错误的数据吗?完全没有数据?获得白页?你检查过你的错误日志吗?