-2

[编辑以获得更好的解释和代码]

你好!我的 web 服务器上有一个 php 脚本,它登录到我的热泵 web 界面 nibeuplink.com 并获取我所有的温度读数等等,并以 json 格式返回它们。

freeboard.io 是一项用于可视化数据的免费服务,因此我正在为我的热泵值制作一个 freeboard.io。在 freeboard.io 中,我可以添加任何 json 数据作为数据源,因此我已将链接添加到我的 php-script。它一次获取数据,但似乎有某种缓存值在此之后使用,因此它们不会使用脚本中的新值进行更新。freeboard.io 使用 get 函数来获取 url。如果我使用普通的 Web 浏览器运行 php 脚本并刷新它,则值会更新 - 并且也会立即在 freeboard.io 中更新。Freeboard.io 有一个设置,每 5 秒自动更新一次数据源。

从我的网络浏览器获取脚本时,似乎有一些东西可以正确触发脚本,但从 freeboard.io 获取时却没有,它每 5 秒使用一次 get 函数来获取新数据。

在 freeboard 中,我可以向 get 请求添加标头,是否有一些标头可以帮助我在这里丢弃任何缓存的数据?

我希望这能更好地解释我的问题。

有什么我可以在开始时添加到我的代码中以始终强制覆盖任何缓存数据的吗?

<?php
/* 
 * read nibe heatpump values from nibeuplink status web page and return them in json format. 
 * based on: https://www.symcon.de/forum/threads/25663-Heizung-Nibe-F750-Nibe-Uplink-auslesen-auswerten 
 * to get the code which is required as parameter, log into nibe uplink, open status page of your heatpump, and check url: 
 * https://www.nibeuplink.com/System/<code>/Status/Overview 
 * 
 * usage: nibe.php?email=<email>&password=<password>&code=<code> 
 */ 

// to add additional debug output to the resulting page: 

$debug = false; 

date_default_timezone_set('Europe/Helsinki');
$date = time();

// Create temp file to store cookies 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 

// URL to login page 
$url = "https://www.nibeuplink.com/LogIn"; 

// Get Login page and its cookies and save cookies in the temp file 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 

// Now you have the cookie, you can POST login values 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 2); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".$_GET['email']."&Password=".$_GET['password']); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); // Uses cookies from the temp file 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects 
$output = curl_exec($ch); 

curl_setopt($ch, CURLOPT_URL, "https://www.nibeuplink.com/System/".$_GET['code']."/Status/ServiceInfo"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_POST, 0); 
$result = curl_exec($ch); 


$pattern = '/<h3>(.*?)<\/h3>\s*<table[^>]*>.+?<tbody>(.+?)<\/tbody>\s*<\/table>/s'; 
if ($debug) echo "pattern: <xmp>".$pattern."</xmp><br>"; 

$pattern2 = '/<tr>\s*<td>(.+?)<span[^>]*>[^<]*<\/span>\s*<\/td>\s*<td>\s*<span[^>]*>([^<]*)<\/span>\s*<\/td>\s*<\/tr>/s'; 
if ($debug) echo "pattern2: <xmp>".$pattern2."</xmp><br>"; 

preg_match_all($pattern, $result, $matches); 

// build json format from matches 
echo '{'; 
$first = true; 
foreach ($matches[1] as $i => $title) { 
        echo ($first ? '"' : ',"').trim($title).'":{'; 
        $content = $matches[2][$i]; 
        preg_match_all($pattern2, $content, $values); 
        $nestedFirst = true; 
        foreach ($values[1] as $j => $field) { 
                echo ($nestedFirst ? '"' : ',"').trim($field).'":"'.$values[2][$j].'"'; 
                $nestedFirst = false; 
        } 
        echo "}"; 
        $first = false; 
} 

echo ",\"time\":{\"Last fetch\":\"$date\"}";

echo "}";

  

if ($debug) { 
        echo "<pre><xmp>"; 
        echo print_r($matches); 
        echo "<br><br>"; 
        echo $result; 
        echo "</xmp></pre>"; 
} 
?>

4

1 回答 1

0

您可以对 php 脚本进行 ajax 调用以刷新网页的一部分。我不明白 io 是什么意思,即您是在谈论从数据库中获取数据,如果数据库中发生任何更改,则只能获取新添加的记录。如果你的意思是这个意思,那么你可以使用 cookie 来跟踪添加到数据库中的任何新记录,并且只有当它找到新记录时,它才能对 php 脚本进行 ajax 调用以在获取的总数据集上运行你的算法。

于 2018-03-02T18:22:32.817 回答