Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Celcius for Simple MSN Weather

    I wish to implement the Simple MSN Weather PHP code to my website but I want the readout to be in Celcius rather than Farrenheit. Has anyone done this or know what I need to do to the following script to make it happen?

    Code:
    <?php
    
    if (!$accid) {
    $accid="USHI0026";
    }
    
    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";
    
    # Use cURL to get the page
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $CurlContent = curl_exec ($ch);
    curl_close ($ch);
    
    # Look for fieldnames, set into $WeatherFields array
    $FieldPattern = '|this.sw([^\s]+) = |';
    preg_match_all($FieldPattern, $CurlContent, $WeatherFields);
    
    # Parse data into hash
    foreach ($WeatherFields[1] as $WeatherField) {
        $FieldSpec = '|this.sw' . $WeatherField . ' = "([^\"]+)|';
        preg_match($FieldSpec, $CurlContent, $WeatherData);
        $MsnWeather[$WeatherField] = $WeatherData[1];
    }
    
    ## DEBUG
    ## print_r($MsnWeather);
    
    print "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\">";
    print "<tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/". $MsnWeather['CIcon'] . ".gif\">  <strong>". $MsnWeather['Temp'] ."°F </strong></td></tr></table>";
    
    ?>

  • #2
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have added some conversion

    PHP Code:
    $MsnWeather['Temp'] = $MsnWeather['Temp']-32;
    $MsnWeather['Temp'] = $MsnWeather['Temp']/9;
    $MsnWeather['Temp'] = $MsnWeather['Temp']*5;
    $MsnWeather['Temp'] = round($MsnWeather['Temp'], 1); 

    So overall code is...

    PHP Code:
    <?php

    if (!$accid) {
    $accid="USHI0026";
    }

    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";

    # Use cURL to get the page
    $ch curl_init();
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
    $CurlContent curl_exec ($ch);
    curl_close ($ch);

    # Look for fieldnames, set into $WeatherFields array
    $FieldPattern '|this.sw([^\s]+) = |';
    preg_match_all($FieldPattern$CurlContent$WeatherFields);

    # Parse data into hash
    foreach ($WeatherFields[1] as $WeatherField) {
        
    $FieldSpec '|this.sw' $WeatherField ' = "([^\"]+)|';
        
    preg_match($FieldSpec$CurlContent$WeatherData);
        
    $MsnWeather[$WeatherField] = $WeatherData[1];
    }

    ## DEBUG
    ## print_r($MsnWeather);

    $MsnWeather['Temp'] = $MsnWeather['Temp']-32;
    $MsnWeather['Temp'] = $MsnWeather['Temp']/9;
    $MsnWeather['Temp'] = $MsnWeather['Temp']*5;
    $MsnWeather['Temp'] = round($MsnWeather['Temp'], 1);

    print 
    "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\">";
    print 
    "<tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['Temp'] ."°C </strong></td></tr></table>";

    ?>

    Should work..

  • #3
    New Coder
    Join Date
    May 2003
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much for that but I am getting values with a decimal. eg. 7.8°C

    It looks like you attempted to rectify this through this part of the code you added:

    Code:
    $MsnWeather['Temp'] = round($MsnWeather['Temp'], 1);
    However, it doesn't seem to work.

    What else needs to be done?

    Thanks...

  • #4
    New Coder
    Join Date
    May 2003
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I worked it out... this is the fix:

    Code:
    $MsnWeather['Temp'] = round($MsnWeather['Temp'], 0);

  • #5
    Regular Coder
    Join Date
    Aug 2005
    Posts
    252
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Cool script. Can I throw a wrench into it?

    How would you combine the two so that you could display both fahrenheit and celcius?

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    First let me say that I'm quite jealous if you are in Honolulu .
    But didn't the original script already correctly find Farrenheit? If so just save the farenheit temperature before its converted. Like this,
    PHP Code:
    #Record the farenheit temperature.
    $MsnWeather['FTemp'] = $MsnWeather['Temp'];

    #Convert farenheit to celcius.
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32
    $MsnWeather['CTemp'] = $MsnWeather['Temp']/9
    $MsnWeather['CTemp'] = $MsnWeather['Temp']*5
    $MsnWeather['CTemp'] = round($MsnWeather['Temp'], 0); 




    So overall code is...

    PHP Code:
    <?php 

    if (!$accid) { 
    $accid="USHI0026"


    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid"

    # Use cURL to get the page 
    $ch curl_init(); 
    curl_setopt($chCURLOPT_URL$url); 
    curl_setopt ($chCURLOPT_RETURNTRANSFER1); 
    $CurlContent curl_exec ($ch); 
    curl_close ($ch); 

    # Look for fieldnames, set into $WeatherFields array 
    $FieldPattern '|this.sw([^\s]+) = |'
    preg_match_all($FieldPattern$CurlContent$WeatherFields); 

    # Parse data into hash 
    foreach ($WeatherFields[1] as $WeatherField) { 
        
    $FieldSpec '|this.sw' $WeatherField ' = "([^\"]+)|'
        
    preg_match($FieldSpec$CurlContent$WeatherData); 
        
    $MsnWeather[$WeatherField] = $WeatherData[1]; 


    ## DEBUG 
    ## print_r($MsnWeather); 

    #Record the farrenheit temperature.
    $MsnWeather['FTemp'] = $MsnWeather['Temp'];

    #Convert Farrenheit to Celcius.
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32
    $MsnWeather['CTemp'] = $MsnWeather['Temp']/9
    $MsnWeather['CTemp'] = $MsnWeather['Temp']*5
    $MsnWeather['CTemp'] = round($MsnWeather['Temp'], 0);

    print 
    "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\"><tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['CTemp'] ."°C </strong></td></tr><tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['FTemp'] ."°F </strong></td></tr></table>"

    ?>
    Whereever you want to use your Farrenheit temperature print $MsnWeather['FTemp'];.
    I believe this should work, however don't quote me. I can't test this code.
    Last edited by StupidRalph; 02-17-2006 at 05:41 AM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    However, it doesn't seem to work.
    It works fine... it was meant to return it with a decimel

    but you wanting it as a hole number, your fix will work fine...

    Whereever you want to use your Farrenheit temperature print $MsnWeather['FTemp'];.
    I believe this should work, however don't quote me. I can't test this code.
    I've read through, all looks good but havn't tested it

  • #8
    Regular Coder
    Join Date
    Aug 2005
    Posts
    252
    Thanks
    23
    Thanked 0 Times in 0 Posts
    For some reason, the script is displaying farenheit for both...no celsius.

    Thoughts?

  • #9
    New Coder
    Join Date
    May 2003
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've also noticed that when MSN put out a "N/A" as the current condition you get a red cross "image not found" square as the icon. Anybody know what that is all about?

  • #10
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Problem seems to be...

    PHP Code:
    #Convert Farrenheit to Celcius. 
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32;  
    $MsnWeather['CTemp'] = $MsnWeather['Temp']/9;  
    $MsnWeather['CTemp'] = $MsnWeather['Temp']*5;  
    $MsnWeather['CTemp'] = round($MsnWeather['CTemp'], 0); 
    Should be...

    PHP Code:
    #Convert Farrenheit to Celcius. 
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32;  
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']/9;  
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']*5;  
    $MsnWeather['CTemp'] = round($MsnWeather['CTemp'], 0); 
    So overall code...
    PHP Code:
    <?php  

    if (!$accid) {  
    $accid="USHI0026";  
    }  

    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";  

    # Use cURL to get the page  
    $ch curl_init();  
    curl_setopt($chCURLOPT_URL$url);  
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);  
    $CurlContent curl_exec ($ch);  
    curl_close ($ch);  

    # Look for fieldnames, set into $WeatherFields array  
    $FieldPattern '|this.sw([^\s]+) = |';  
    preg_match_all($FieldPattern$CurlContent$WeatherFields);  

    # Parse data into hash  
    foreach ($WeatherFields[1] as $WeatherField) {  
        
    $FieldSpec '|this.sw' $WeatherField ' = "([^\"]+)|';  
        
    preg_match($FieldSpec$CurlContent$WeatherData);  
        
    $MsnWeather[$WeatherField] = $WeatherData[1];  
    }  

    ## DEBUG  
    ## print_r($MsnWeather);  

    #Record the farrenheit temperature. 
    $MsnWeather['FTemp'] = $MsnWeather['Temp']; 

    #Convert Farrenheit to Celcius. 
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32;  
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']/9;  
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']*5;  
    $MsnWeather['CTemp'] = round($MsnWeather['CTemp'], 0); 

    print 
    "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\"><tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['CTemp'] ."°C </strong></td></tr><tr><td><img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['FTemp'] ."°F </strong></td></tr></table>";  

    ?>
    The mess up seems to come from StupidRalph's addittion to the code

  • #11
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've also noticed that when MSN put out a "N/A" as the current condition you get a red cross "image not found" square as the icon. Anybody know what that is all about?
    Give this a whizz.. added some checks to see if MSN weather has returned "N/A", if it has, dont print an img but print "N/A".

    PHP Code:
    <?php   

    if (!$accid) {   
    $accid="USHI0026";   
    }   

    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";   

    # Use cURL to get the page   
    $ch curl_init();   
    curl_setopt($chCURLOPT_URL$url);   
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);   
    $CurlContent curl_exec ($ch);   
    curl_close ($ch);   

    # Look for fieldnames, set into $WeatherFields array   
    $FieldPattern '|this.sw([^\s]+) = |';   
    preg_match_all($FieldPattern$CurlContent$WeatherFields);   

    # Parse data into hash   
    foreach ($WeatherFields[1] as $WeatherField) {   
        
    $FieldSpec '|this.sw' $WeatherField ' = "([^\"]+)|';   
        
    preg_match($FieldSpec$CurlContent$WeatherData);   
        
    $MsnWeather[$WeatherField] = $WeatherData[1];   
    }   

      
    # If the temp is N/A...
    if($MsnWeather[Temp] == "N/A"){
       
    $notavailabletemp "1";
    }


    #Record the farrenheit temperature.  
    $MsnWeather['FTemp'] = $MsnWeather['Temp'];  

    #Convert Farrenheit to Celcius.  
    if($notavailabletemp == 1){
       
    $MsnWeather['CTemp'] = $MsnWeather['Temp'];
    }else{
       
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32;   
       
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']/9;   
       
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']*5;   
       
    $MsnWeather['CTemp'] = round($MsnWeather['CTemp'], 0);  
    }

    print 
    "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\"><tr><td>";

    if(
    $notavailabletemp == 1){
       echo 
    "N/A";
    }else{
       echo 
    "<img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">";
    }

    echo 
    "<strong>"$MsnWeather['CTemp'] ."°C </strong></td></tr><tr><td>";

    if(
    $notavailabletemp == 1){
       echo 
    "N/A";
    }else{
    echo 
    "<img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['FTemp'] ."°F </strong></td></tr></table>";   
    }

    ?>

  • #12
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by Errica
    For some reason, the script is displaying farenheit for both...no celsius.

    Thoughts?
    Your not getting both? I just copied the above script and it returned both for me. The only problem I did have was on line 3 from the original code.
    PHP Code:
    if (!$accid)  { 
    was coming up undefined, it should be
    PHP Code:
    if (!isset($accid) { 
    As far as the image....It doesn't show up in the first original code posted, alteast not for me. Therefore, my code isn't going to show it either. It is looking for the image in a relative path called (icons/30.gif) so if you don't have a folder named (icons) on your local machine with (30.gif) inside....you are going to get that error.

    Also, I see that I am returning Undefined offset on line 21. :1
    Is WeatherFields[1] and array? If not, then foreach has a valid reason to complain.

    The only thing I did in my code was introduce a new variable to save the farenheit temperture before it is converted. However, I did forget to change the last few lines in the conversion from Weather[Temp] to Weather[CTemp] but Goughy took care of that already I typically do all my conversions on one line utilizing PEMDAS.
    PHP Code:
    $num 2//num equals 2
    $num2$num;  //num2 now equals 2
    $num2 = ($num2 +1) * 3//2 + 1 times 3 === num2 should now yield 9 
    However I did add a new <tr> with the would be Celcius temp just to show you how to print it out.
    Last edited by StupidRalph; 02-20-2006 at 10:02 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #13
    New Coder
    Join Date
    May 2003
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Can you change it so rather than printing "N/A" you get the following icon:

    images/icons/44.gif

    Which gives you:

  • #14
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    I think we are working under different conditions. When I tried goughy's code. This is what I got (check view attachment). It returns both temp. But also a few errors. Are you guys not getting this?
    Attached Thumbnails Attached Thumbnails Celcius for Simple MSN Weather-goughy_code.gif  
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #15
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    If what goughy had worked for you...all you have to do is where goughy echoed out "N/A" print the img src for the icon like this.
    PHP Code:
    <?php    

    if (!$accid) {    
    $accid="USHI0026";    
    }    

    $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";    

    # Use cURL to get the page    
    $ch curl_init();    
    curl_setopt($chCURLOPT_URL$url);    
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);    
    $CurlContent curl_exec ($ch);    
    curl_close ($ch);    

    # Look for fieldnames, set into $WeatherFields array    
    $FieldPattern '|this.sw([^\s]+) = |';    
    preg_match_all($FieldPattern$CurlContent$WeatherFields);    

    # Parse data into hash    
    foreach ($WeatherFields[1] as $WeatherField) {    
        
    $FieldSpec '|this.sw' $WeatherField ' = "([^\"]+)|';    
        
    preg_match($FieldSpec$CurlContent$WeatherData);    
        
    $MsnWeather[$WeatherField] = $WeatherData[1];    
    }    

       
    # If the temp is N/A... 
    if($MsnWeather[Temp] == "N/A"){ 
       
    $notavailabletemp "1"



    #Record the farrenheit temperature.   
    $MsnWeather['FTemp'] = $MsnWeather['Temp'];   

    #Convert Farrenheit to Celcius.   
    if($notavailabletemp == 1){ 
       
    $MsnWeather['CTemp'] = $MsnWeather['Temp']; 
    }else{ 
       
    $MsnWeather['CTemp'] = $MsnWeather['Temp']-32;    
       
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']/9;    
       
    $MsnWeather['CTemp'] = $MsnWeather['CTemp']*5;    
       
    $MsnWeather['CTemp'] = round($MsnWeather['CTemp'], 0);   


    print 
    "<table onmouseover=\"this.style.cursor='pointer'; return true;\" onmouseout=\"return true;\" onClick=\"top.location.href='http://weather.msn.com/local.aspx?wealocations=wc:USHI0026'\"><tr><td>"

    if(
    $notavailabletemp == 1){ 
       echo 
    "N/A"
    }else{ 
       echo 
    "<img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">"


    echo 
    "<strong>"$MsnWeather['CTemp'] ."°C </strong></td></tr><tr><td>"

    if(
    $notavailabletemp == 1){ 
       echo 
    "<img src=\"icons/44.gif\">"
    }else{ 
    echo 
    "<img align=\"absmiddle\" border=\"0\" src=\"icons/"$MsnWeather['CIcon'] . ".gif\">  <strong>"$MsnWeather['FTemp'] ."°F </strong></td></tr></table>";    


    ?>
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •