如何从网页上进行数据挖掘的PHP脚本工作?

问题描述 投票:-2回答:1

[编辑更好的解释和代码包括]

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

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

似乎有些东西在从我的Web浏览器中获取时会正确触发脚本,但是当从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>"; 
} 
?>
php json freeboard
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.