我的PHP程序赢得了';t存储我的股票信息,它会给我一些警告

我的PHP程序赢得了';t存储我的股票信息,它会给我一些警告,php,Php,我正在运行一个PHP程序,通过他们的API代码来存储网站上的信息,为此我制作了两个文件;“db.JSON”和“index.php”。数据库文件为空,只有两个括号“[]”,我的索引文件代码如下所示: while(true){ $dataSource=json\u decode(文件\u获取\u内容(“https://www.mywebsite.dk/api/ticker/"(对),; 打印(数据源); $db=json\u decode(file\u get\u contents(“db.json

我正在运行一个PHP程序,通过他们的API代码来存储网站上的信息,为此我制作了两个文件;“db.JSON”和“index.php”。数据库文件为空,只有两个括号“[]”,我的索引文件代码如下所示:

while(true){
$dataSource=json\u decode(文件\u获取\u内容(“https://www.mywebsite.dk/api/ticker/"(对),;
打印(数据源);
$db=json\u decode(file\u get\u contents(“db.json”),true);
数组推送($db,$dataSource);
文件内容(“db.json”,json编码($db));
睡眠(6);
}
但是当我运行
php'/home/eml/Dropbox/ElementaryOS/Bit2/index.php'
时,我得到以下错误:

Array
(
    [timestamp] => 1455739785
    [result]    => 43
)
PHP Warning: file_get_contents(db.json): failed to open stream: No such file or directory in /home/emil/Dropbox/ElementaryOS/Bit2/index.php on line 5

PHP Warning: array_push() expects parameter 1 to be array, null given in /home/emil/Dropbox/ElementaryOS/Bit2/index.php on line 6
Array
(
    [timestamp] => 1455739796
    [result] => 44
)

PHP Warning: array_push() expects parameter 1 to be array, null given in /home/emil/Dropbox/ElementaryOS/Bit2/index.php on line 6
Array
(
    [timestamp] => 1455739806
    [result] => 44
)
诸如此类,你就明白了


db.json
文件位于我的索引文件所在的文件夹中。因此,我一直在互联网上不同的地方寻找一些答案来解决这个问题,包括这个论坛和我发现的似乎无法解决问题的东西。

我认为在定义$db时,您可能需要更加小心。如果文件尚不存在,或者数据为空或已损坏,该怎么办?我将添加一些验证并检查文件_是否存在()



另外,确保文件是可写的,并且您要写入的目录是可由PHP(Apache/Server)写入的。您可能还需要使用指向正在使用的文件的完整路径。

PHP找不到您的
db.json
文件-请尝试输入完整路径,确保权限正确,等等。哎呀。。。应该是(文件_存在(“db.json”){…}我忘了关闭)
<?php
   while(true) {
   $dataSource = json_decode(file_get_contents("https://www.mywebsite.dk/api/ticker/"), true);
   print_r($dataSource);
   if (file_exists("db.json")) {
       $db = json_decode(file_get_contents("db.json"), true);
   } else {
       $db = array();
   }
   if (!is_array($db) || empty($db)) {
       $db = array();
   }
   array_push($db, $dataSource);
   file_put_contents("db.json", json_encode($db));
   sleep(6);
}
?>