46 users online (0 members and 46 guests)  


  Results 1 to 10 of 10

Related

  1. form data displayed on html page    Forum: PHP Forum
    Replies: 1
  2. Replies: 3
  3. Get data in same HTML page    Forum: HTML Forum
    Replies: 1
  1. #1
    kronflux's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    5

    grabbing weather data from web page..

    okay, so I'm not by any means a pro at PHP. I'm actually very much a beginner. heres what I'm trying to do.. I'm trying to make sort of a widget for my desktop that displays a local php document(yes, I'm running apache and such, so it does work.)
    and I want it to have local weather in it.
    specifically from a particular website.
    so far, I have it working great. but the problem is.. it displays the info in Fahrenheit. I need it in Celsius. there's a link on the page I'm grabbing from which allows you to change some sort of custom setting, and allows display in whichever measurement you want, but as I'm grabbing it and displaying it locally, this link does not work.
    heres what I have:

    Code:
    <?php
    
    $url = "http://www.timeanddate.com/weather/canada/halifax";
    $file = file_get_contents($url);
    $word1='<th>Current<br />conditions</th>';
    $word2='<a href="/weather/canada">Weather in rest of Canada</a>';
    $fh = fopen('test.txt', 'w');
    fwrite($fh,$file);
    fclose($fh);
    
    $handle = @fopen('test.txt', "rt");
    $contents = fread($handle, filesize('test.txt'));
    fclose($handle);
    
    $between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));
    
    echo $between;
    ?>
    yes, I know there are better ways of going about this, and I welcome any of those.. I just didn't know how to proceed, I got a lot of errors, then gave up and did it this way.

    point is.. you can look at the link:
    http://www.timeanddate.com/weather/canada/halifax
    and see what I've got, and maybe theres some way to activate that custom setting from php? or.. um.. a way to extract the numbers from the info, and convert them to celsius? I dunno. I just need help

    thanks in advance!
    Last edited by HTML; 03-15-2009 at 08:50 AM.

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: grabbing weather data from web page..

    Try opening the following page instead:

    http://www.timeanddate.com/custom/sa...canada/halifax

  3. #3
    kronflux's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    5

    Re: grabbing weather data from web page..

    yeah, I've tried that too. it brings up a blank page.
    maybe i need to try another way of getting the page?
    whats happening when you load
    http://www.timeanddate.com/custom/sa...canada/halifax
    instead of
    http://www.timeanddate.com/weather/canada/halifax
    is that in your browser, it stores a cookie called TIMEANDDATE from the site timeanddate.com
    its content is "fvn_1:fun_1", which means "fun" being at value "1", is Celsius, and "fun" being at value "2" is Fahrenheit.
    is there a way to get this script to load the main page using a cookie?
    Last edited by kronflux; 03-16-2009 at 02:24 PM.

  4. #4
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: grabbing weather data from web page..

    If you view the source of the page, the following comment is right at the top:
    Code:
    <!--
    scripts and programs that download content transparent to the user are not allowed without permission
    -->
    Something they have on their end may be blocking your ability to view the right code. However, if you are able to extract the Farenheit data without issues, why not convert it yourself?

  5. #5
    kronflux's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    5

    Re: grabbing weather data from web page..

    well what I was thinking is.. either what I just suggested, with loading the page with that cookie data, or to extract the numbers from the script, convert them to Fahrenheit, then replace them as Celsius.

  6. #6
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: grabbing weather data from web page..

    Look into using cURL. It allows you to use cookies, etc., and should let you do what you are looking to do.

  7. #7
    kronflux's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    5

    Re: grabbing weather data from web page..

    I'll give that a look, if anyone has any specific info on how I could do that in my particular case, it would be greatly appreciated ^_^

  8. #8
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: grabbing weather data from web page..

    I have not tested the following code, but it is adapted from sone that I use to automatically grab data from a page in order to run reports.

    PHP Code:
        $tmpFile "tmp.txt";
        
    $temp "temp.txt";
            
    $gacookie "cookiefile";

            
    ### Initialize cURL session ###
            
    $curl curl_init();

            
    ### Log in to Authorize.net ###
            
    $fp fopen($temp"w");
            
    curl_setopt($curlCURLOPT_URL,"http://www.timeanddate.com/custom/save.php?mode=patch&fun=1&ref=/weather/canada/halifax");
            
    curl_setopt($curlCURLOPT_SSL_VERIFYPEERFALSE);
            
    curl_setopt($curlCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
            
    curl_setopt($curlCURLOPT_TIMEOUT60);
            
    curl_setopt($curlCURLOPT_FOLLOWLOCATION1);
            
    curl_setopt($curlCURLOPT_RETURNTRANSFER1);
            
    curl_setopt($curlCURLOPT_COOKIEJAR$gacookie);
            
    curl_setopt($curlCURLOPT_HEADER0);
            
    curl_setopt($curlCURLOPT_FILE$fp);
            
    curl_setopt($curlCURLOPT_POST1);
            
    $xxx curl_exec($curl);
            
    fclose($fp);

            
    ### Get list of available dates ###
            
    $fp fopen($tmpFile"w");
            
    curl_setopt($curlCURLOPT_URL"http://www.timeanddate.com/weather/canada/halifax");
            
    curl_setopt($curlCURLOPT_COOKIEFILE$gacookie);
            
    curl_setopt($curlCURLOPT_FILE$fp);
            
    $xxx curl_exec($curl);
            
    fclose($fp);

            
    ### Close cURL session ###
            
    curl_close ($curl); 

  9. #9
    kronflux's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    5

    Re: grabbing weather data from web page..

    it works! thanks a lot!

  10. #10
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: grabbing weather data from web page..

    Glad to be of assistance. Sorry it took a bit to figure it out.



Tags for this Thread