Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当时间戳在php中不同时才输出内容_Php - Fatal编程技术网

仅当时间戳在php中不同时才输出内容

仅当时间戳在php中不同时才输出内容,php,Php,我有一个php脚本,它作为cronjob每分钟运行一次,以json格式获取url的内容,并实时输出内容 json格式的API: [ { "timestamp": { "unix": 123456789 }, "content": "Lorem ipsum" } ] PHP脚本: $json = file_get_contents($url); $obj = json_decode($jso

我有一个php脚本,它作为cronjob每分钟运行一次,以json格式获取url的内容,并实时输出内容

json格式的API:

[
    {
        "timestamp": {
            "unix": 123456789        
        },
        "content": "Lorem ipsum"
    }
]
PHP脚本:

$json = file_get_contents($url);

$obj = json_decode($json);

// if timestamp is different, output the content.

echo $obj[0]->content;
因为脚本每分钟都通过cronjob运行,所以如果时间戳没有改变,我总是每分钟都会得到相同的重复内容

我只想在时间戳改变时输出内容,这样我就不会在cronjob每分钟运行时都得到重复的内容

如何在php中实现这一点

在其他语言中,我有一个类似的问题,但我使用下面的伪代码解决了它:

declare static_time and initialize to zero;
if static_time is not equal the current_time
run the code
then set static_time again to current_time
但我不知道如何在PHP中实现相同的解决方案,或者可能有更好的方法在PHP中解决它而不使用数据库

非常感谢您的帮助


谢谢

我知道您正在使用数据库。那么,为什么不使用唯一列作为键,然后可以首先检查数据库中是否已经有时间戳…@MaxMuster我没有数据库。如果使用数据库,这很容易,但在这种情况下,脚本没有数据库。您在某个文件夹中有写权限吗?我知道您正在使用数据库。那么,为什么不使用唯一列作为键,然后可以首先检查数据库中是否已经有时间戳…@MaxMuster我没有数据库。如果使用数据库,这很容易,但在这种情况下,脚本没有数据库。您在某个文件夹中有写权限吗?