Page 1 of 1

Fetching Dynamic Data from Another Web Page

Posted: Tue Feb 09, 2010 11:09 am
by techkid
How can I fetch Dynamic Data from Another Web Page.

Here is what I exactly want to do.

Codes on another website:

Code: Select all

 
other tr and stuff
<tr> 
<td width="100" height="18"><b>Temperature</b></td> 
<td width="75">Some Dynamic Output</td> 
<td width="80">Some Dynamic Output</td> 
</tr> 
other tr and stuff
 
Is there a possible way of fetching this to my webpage using PHP?

There is no RSS Feed for that website.

Re: Fetching Dynamic Data from Another Web Page

Posted: Tue Feb 09, 2010 11:30 am
by a.heresey
Umm, maybe you could load the page as a dom (if it's xhtml) and then traverse the tree to get at what you want?

Re: Fetching Dynamic Data from Another Web Page

Posted: Tue Feb 09, 2010 11:38 am
by techkid
a.heresey wrote:Umm, maybe you could load the page as a dom (if it's xhtml) and then traverse the tree to get at what you want?
I don't understand, Can you explain a bit more.

I'm trying to get data on this page:

Code: Select all

http://www.meteorology.gov.mv/?pd=mapst ... l&tag=male

Re: Fetching Dynamic Data from Another Web Page

Posted: Tue Feb 09, 2010 11:45 am
by Grizzzzzzzzzz
yes there is, it's called page scraping

http://www.bradino.com/php/screen-scraping/

a very quick and filthy example:

Code: Select all

 
<?php
 
$url = "http://www.meteorology.gov.mv/?pd=mapstuff&id=html&tag=male";
 
$raw = file_get_contents($url);
 
list($part1, $part2) = explode("Temperature</b></td>", $raw);
list($part1, $part2) = explode("</tr>", $part2);
 
echo "<table border='1'>";
echo "<tr>";
echo "<td>Temperature</td>";
echo "</tr>";
echo "<tr>";
echo $part1;
echo "</tr>";
echo "</table>";
 
?>
 

Re: Fetching Dynamic Data from Another Web Page

Posted: Tue Feb 09, 2010 3:05 pm
by klevis miho
Do this:

Code: Select all

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.meteorology.gov.mv/?pd=mapstuff&id=html&tag=male');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
 
preg_match_all('#<tr>(.+?)</tr>#s', $contents, $matches);
$matches = $matches[0];
 
//show whats in the $matches array
print_r($matches);

Here you get all the contents that are inside <tr> and </tr> (including them)

Re: Fetching Dynamic Data from Another Web Page

Posted: Wed Feb 10, 2010 1:30 am
by techkid
klevis miho wrote:Do this:

Code: Select all

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.meteorology.gov.mv/?pd=mapstuff&id=html&tag=male');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
 
preg_match_all('#<tr>(.+?)</tr>#s', $contents, $matches);
$matches = $matches[0];
 
//show whats in the $matches array
print_r($matches);

Here you get all the contents that are inside <tr> and </tr> (including them)
Now there are many arrays.

How about this one only?

Code: Select all

           [11] =>  
          <td width="100" height="18"><b>Temperature</b></td> 
          <td width="75">30&#176; C</td> 
          <td width="80">86&#176; F</td> 
 

Re: Fetching Dynamic Data from Another Web Page

Posted: Wed Feb 10, 2010 1:34 am
by klevis miho
This will be $matches[11];

echo $matches[11];