Getting data from another site with php via ID

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:07:34

问题


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

EDIT: I found the code here: Fetching data from another website with php

EDIT: 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";

?>

OUTPUT: EUR: USD: GBP:

Can anyone give me any advice?


回答1:


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


?>

Output

EUR: 545.33 USD: 407.48 GBP: 633.63


来源:https://stackoverflow.com/questions/18146608/getting-data-from-another-site-with-php-via-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!