我需要从本地网络上运行的站点获取数据。 这将用于显示我们的太阳能电池板上的读数。
这是我从以下网址获取数据所需的:
<td>Gesamtertrag:</td>
<!--<td><input type="text" size="8" id="E_Total" readonly /></td>-->
<td><div id="E_Total"></div>
<td><div id="uE_Total"></div></td>
<td>P AC:</td>
<!--<td><input type="text" size="8" id="P_AC" readonly /></td>-->
<td><div id="P_AC"></div>
<td><div id="uP_AC"></div></td>
我一直在尝试修改下面的代码,以使其工作但没有成功:
<?php
$content = file_get_contents("http://www.cba.am/am/SitePages/Default.aspx");
preg_match('#<b>USD</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $USDmatch);
preg_match('#<b>EUR</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $EURmatch);
preg_match('#<b>GBP</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $GBPmatch);
$eur = $EURmatch[3];
$usd = $USDmatch[3];
$gbp = $GBPmatch[3];
echo "EUR: $eur USD: $usd GBP: $gbp";?>
编辑:我在这里找到了代码:Fetching data from another website with php
编辑:我修改了Nathan发布的代码,但它根本没有给我任何数据。 <?php
$content = "http://172.25.205.56/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $content);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$body= curl_exec ($ch);
curl_close ($ch);
preg_match('#<td><div id="E_Total">([0-9\.]*)</div><div id="E_Total">([0-9\.]*)</div>#Uis', $body, $USDmatch);
preg_match('#<b>EUR</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $body, $EURmatch);
preg_match('#<b>GBP</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $body, $GBPmatch);
$eur = $EURmatch[3];
$usd = $USDmatch[3];
$gbp = $GBPmatch[3];
echo "EUR: $eur USD: $usd GBP: $gbp";
?>
输出:EUR: USD: GBP:
有人可以给我任何建议吗?
答案 0 :(得分:1)
使用Curl打算使用file_get_contents
<?php
$content = "http://www.cba.am/am/SitePages/Default.aspx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $content);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$body= curl_exec ($ch);
curl_close ($ch);
preg_match('#<b>USD</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $body, $USDmatch);
preg_match('#<b>EUR</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $body, $EURmatch);
preg_match('#<b>GBP</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $body, $GBPmatch);
$eur = $EURmatch[3];
$usd = $USDmatch[3];
$gbp = $GBPmatch[3];
echo "EUR: $eur USD: $usd GBP: $gbp";
?>
输出
EUR: 545.33 USD: 407.48 GBP: 633.63