gpt4 book ai didi

javascript - 如果发现 json 文件发生更改,则发送短信

转载 作者:行者123 更新时间:2023-12-03 09:41:07 27 4
gpt4 key购买 nike

我有这个 json 文件,我需要为其创建 cron 作业并在数据更改时接收短信通知 http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json

我需要查明某些服务器是否有库存,所以我找到了以下代码:

<?php

$cellphone = '15551234567';

$a_track = array('143sys12');

$s = file_get_contents('http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json');

$tmp = json_decode($s, true);
$a = $tmp ['availability'];

$data = array();

foreach ($a as $item) {
if (!in_array($item ['reference'], $a_track)) {
continue;
}
foreach ($item ['zones'] as $zone) {
if ($zone ['availability'] == 'unavailable') {
continue;
}
$data [$item ['reference']] .= $zone ['zone'];
}
}

foreach ($data as $item => $availability) {
$message = "SYS STOCK: $item: $availability";
mail('my@email', 'OMG BUY THIS NOW!', $message);
$url = "http://rest.nexmo.com/sms/json?api_key=xxxx&api_secret=yyyy& from=nnnnnnnn&to=$cellphone&text=" . urlencode($message);
$discard = file_get_contents($url);
}

问题是,当我触发它时,无论服务器是否有库存,我都会收到短信,并且短信不断出现误报。我也收到了这条消息:

]# /usr/bin/php /home/sys.php
PHP Notice: Undefined index: 143sys12 in /home/sys.php on line 22

最佳答案

我稍微更改了您的代码,并将消息的发送移到了一个函数中,以便在找到引用时调用。

看一下:

<?php

$a_track = array('143sys12');

$s = file_get_contents('http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json');

$tmp = json_decode($s, true);

foreach ($tmp['availability'] as $item) {
if (in_array($item['reference'],$a_track)) sendMeAnEmail($item);
}

function sendMeAnEmail($item){
$cellphone = '15551234567';

$message = "SYS STOCK: ". $item["reference"] . PHP_EOL;
$gotStock = 0;

foreach ($item["zones"] as $zone)
if ($zone["availability"] != "unavailable" and $zone['availability'] != 'unknown' ) {
$message .= "Available in the " . $zone["zone"]. " zone, with the " . $zone["availability"] . " status." ;
$gotStock++;
}

$url = "http://rest.nexmo.com/sms/json?api_key=xxxx&api_secret=yyyy&from=nnnnnnnn&to=$cellphone&text=" . urlencode($message);

if ($gotStock > 0) {
print $message . PHP_EOL; // to check
mail('my@email', 'OMG BUY THIS NOW!', $message);
$discard = file_get_contents($url);
print "Got stock, sent mail and SMS" . PHP_EOL;
}
}

关于javascript - 如果发现 json 文件发生更改,则发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com