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
techkid
Forum Commoner
Posts: 54 Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives
Post
by techkid » Tue Feb 09, 2010 11:09 am
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.
a.heresey
Forum Commoner
Posts: 59 Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US
Post
by a.heresey » Tue Feb 09, 2010 11:30 am
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?
techkid
Forum Commoner
Posts: 54 Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives
Post
by techkid » Tue Feb 09, 2010 11:38 am
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
Grizzzzzzzzzz
Forum Contributor
Posts: 125 Joined: Wed Sep 02, 2009 8:51 am
Post
by Grizzzzzzzzzz » Tue Feb 09, 2010 11:45 am
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:
Post
by klevis miho » Tue Feb 09, 2010 3:05 pm
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)
techkid
Forum Commoner
Posts: 54 Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives
Post
by techkid » Wed Feb 10, 2010 1:30 am
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° C</td>
<td width="80">86° F</td>
klevis miho
Forum Contributor
Posts: 413 Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:
Post
by klevis miho » Wed Feb 10, 2010 1:34 am
This will be $matches[11];
echo $matches[11];