Jump to content

Fetching Weather in php


nitrus

Recommended Posts

Hello PHP Freaks Forums,

 

I am trying to construct a web app to fetch weather off "http://www.flightstats.com", and look up the airport code and display weather for that airport code in that location.  I have some code i started writting that has few errors i need help fixing.  The code fetches weather off of the flightstats.com, but i need to figure out how i could make a drop down box or form that a user can type a airport code and fetch the weather from flight stats or if you have better website that works better please feel free to suggest it.  Thanks.

 

There is error in line three.

<?php
$airport_code = $_GET["airport"];;
    function get_string_between($string, $start, $end)
    {
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0)
            return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }
    $data = file_get_contents("http://www.flightstats.com/go/Airport/weather.do?airportCode=".$airport_code."");

    $weatherdiv = get_string_between($data, '<table width="237" class="contentTable">', '</table>');
    $weathertext = strip_tags($weatherdiv);
    ?>
    
    
<table width="237" class="contentTable">
<?php echo $weathertext; ?>
</table>
 

 

 

Link to comment
Share on other sites

Their Terms of Use prohibits what you're trying to do. With added bolding,

Description of Site and License

 

The Site provides information about FlightStats' products and services. The text, images, and other contents of the Site (collectively, the "Contents") are protected by the copyright laws of the United States and other countries. FlightStats grants you a license to view the Contents on the publicly accessible areas of the Site. This license does not include the right to screen scrape, reproduce, distribute, broadcast, modify, or otherwise commercially exploit any of the Contents, except that reproduction incidental to your use of the Site in accordance with these Terms is permitted. The license can be terminated by FlightStats at any time with or without cause.

Fortunately they do have a developer center you can visit.
Link to comment
Share on other sites

I can't imagine it being hard to find a database of airports (with codes) and their locations. Pair that up with a generic weather service of which I know there are many.

IMO learning how to use a service/API is more useful than how to scrape an HTML page.

 

To answer the question traversing the DOM is a smarter way of doing it than regular expressions, which aren't suited for stuff like HTML in the first place. Unless the HTML is horribly mangled, but it's almost never so bad that it can't be dealt with.

Link to comment
Share on other sites

@nitrus,

 

I don't intend to advertise or self-promote here, but I have a collection of APIs published with Mashape including a weather API that might help you out.

 

https://www.mashape.com/laiello/weather-4

 

Hope it helps.

 

If you need some kind of data not readily available there, PM me and I'll see what I can do.

 

Ok thanks ill see if anything can help me out.  I'm looking for api that fetch airport weather tho.  I will surely take a look!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.