Fetching Dynamic Data from Another Web Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Fetching Dynamic Data from Another Web Page

Post 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.
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

Re: Fetching Dynamic Data from Another Web Page

Post 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?
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Re: Fetching Dynamic Data from Another Web Page

Post 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
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Fetching Dynamic Data from Another Web Page

Post 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>";
 
?>
 
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Fetching Dynamic Data from Another Web Page

Post 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)
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Re: Fetching Dynamic Data from Another Web Page

Post 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> 
 
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Fetching Dynamic Data from Another Web Page

Post by klevis miho »

This will be $matches[11];

echo $matches[11];
Post Reply