通过ID通过php从另一个站点获取数据 [英] Getting data from another site with php via ID

查看:47
本文介绍了通过ID通过php从另一个站点获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从运行在我们本地网络上的站点获取数据.这将用于显示我们太阳能电池板上的读数.

I need to fetch data from a site running on our local network. This will be used to show the readings on our solar panels.

这就是我要从中获取数据的地方:

This is what I need to fetch the data from:

    <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>

我一直在尝试修改下面的代码,以使其正常运行,但没有成功:

I have been trying to modify the code below, in order to make it work but haven't succeded:

<?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";?>

我在这里找到代码:使用php从另一个网站获取数据

我已经修改了内森(Nathan)发布的代码,但是它根本没有提供任何数据.

I have modified the code posted by Nathan, but it just gives me no data at all.

    <?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:

有人可以给我任何建议吗?

Can anyone give me any advice?

推荐答案

使用卷曲打算使用file_get_contents

Use Curl intends of 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

这篇关于通过ID通过php从另一个站点获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆