user2667824
user2667824

Reputation: 11

Getting data from another site with php via ID

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?

Upvotes: 1

Views: 1972

Answers (1)

Padmanathan J
Padmanathan J

Reputation: 4620

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

Upvotes: 1

Related Questions