We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4192
    • 43 Posts
    [Moderator note: In this thread, a bunch of people help Bryn create a snippet to display weather using MODx features. As of 12 July the finished snippet is not generally available.]

    I’d really like to add local weather conditions (data and icon) to my MODx-based website. I have found an excellent plug-in for WordPress called WeatherIcon. You can find more info here. Being a PHP file, I believe it should be possible to adapt it so to make it work with MODx. The problem is that I’m not a PHP expert, so I really don’t know how to do it.
    Anyone is interested in the "translation" of this plug-in for MODx?
      • 4192
      • 43 Posts
      Nobody replied so far. So, probably the easiest way is to include some PHP code to grab the weather data available through RSS feeds like This one from MSN Wather.
      My only problems are:

      • How can I grab those data to make  them appear on a MODx page?
      • How can I translate in another language the terms used on the original RSS feed?
      Any help would be greatly appreciated!
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        http://modxcms.com/forums/index.php/topic,2366.0.html

        [Moderator note: That’s a link to NewsParserX, which takes an RSS feed from another site and displays it]
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 4192
          • 43 Posts
          OK, I succeeded in adapting a simple weather php script to my own requirements. In its basic form, you may have a look here.
          The problem is that I’m not a php expert, so when I tried to transform this (working) php script into a snippet for MODx, I discovered that something went wrong, as you can see here (look at the upper right).

          Does anybody know how can I translate the php script into a working snippet?

          Any help would be greatly appreciated!
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I can only say how I got the feed from these forums on my site. I installed the NewsParserX snippet, then used another snippet which I named ParseNews to actually run NewsParserX:

            return $modx->runSnippet('NewsParserX',array('url'=>'http://modxcms.com/forums/index.php?sa=recent;type=rss2;action=.xml&limit=5'));


            and used the second snippet in my template. I’m still not real sure why I need the second snippet, but I never could get the main NewsParserX to work by itself.

            I tried the url you mentioned above, and with a slight modification got it to work:

            return $modx->runSnippet('NewsParserX',array('url'=>'http://weather.msn.com/RSS.aspx?wealocations=wc:FRXX0129&weadegreetype=C'));


            I’ll leave it up on my site for a while... http://www.sottwell.com
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 4192
              • 43 Posts
              Thank you for the suggestion, but with your parser it seems it’s possible only to get weather info in English (as the original RSS feed is in English). The visitors to the website I’m working on are Italians, so I need a system translated in this language, like the PHP script I’m using
              As you can see, I’ve got a PHP page that is 100% working. Is there anybody that could let me know how can I modify it to let MODx snippet system accept it?

              Thanks
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: Bryn at Mar 05, 2006, 02:39 PM

                Thank you for the suggestion, but with your parser it seems it’s possible only to get weather info in English (as the original RSS feed is in English). The visitors to the website I’m working on are Italians, so I need a system translated in this language, like the PHP script I’m using
                As you can see, I’ve got a PHP page that is 100% working. Is there anybody that could let me know how can I modify it to let MODx snippet system accept it?

                Bryn, feel free to post the code as an text file attachment (just add .txt to the filename) or if it’s brief, use the Insert Code feature to paste the code into the forum. We can help you convert it. wink
                  • 4192
                  • 43 Posts
                  Here is the code: if you succeed in making it work as a MODx snippet, I will translate back to English, so other users may use it.
                  <?php
                  
                  // Adaptation by Diego Meozzi of a php script originally developed by I Ketut Sukandia (www.balidwipa.com and www.nusa-penida.com)
                  
                  if (!$accid) {
                  	$accid="ITXX0056"; # Your default city location code, please change to your city ACCID code.
                  }
                  
                  if (!$tp) {
                  	$tp=C; # Your Default temperature unit (C for Celsius, F for Fahrenheit)
                  }
                  
                  # Fonts Setting
                  ################
                  $fontface="Arial";
                  $fontcolor1="#000000";
                  $fontcolor2="red";
                  $fontsize1="1";
                  $fontsize2="3";
                  $fontsize3="4";
                  
                  function WeatherIndex($tp) {
                  global $accid, $tp;
                  
                  $url ="http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";
                  
                  $fa = fopen($url,"r");
                  			
                  /* $fa = fopen("www.msnbc.com", 80, &$num_error, &$str_error, 30);
                  if(!$fa)
                    { print "Weather is not available: $str_error ($num_error)\n"; }
                  else
                  {
                    fputs($fa,"GET /m/chnk/d/weather_d_src.asp?acid=$accid HTTP/1.0\n\n");
                    $answer=fgets($fa,128);
                  */
                  
                    $v_City    = "";
                    $v_SubDiv  = "";
                    $v_Country = "";
                    $v_Region  = "";
                    $v_Temp    = "";
                    $v_CIcon   = "";
                    $v_WindS   = "";
                    $v_WindD   = "";
                    $v_Baro    = "";
                    $v_Humid   = "";
                    $v_Real    = "";
                    $v_UV      = "";
                    $v_Vis     = "";
                    $v_LastUp  = "";
                    $v_Fore    = "";
                    $v_Acid    = "";
                    $v_ConText = "";
                  
                  $v_Text[] = "dati non disponibili";
                  $v_Text[1] = "piovoso con vento";
                  $v_Text[3] = "piovoso con temporali";
                  $v_Text[4] = "piovoso con temporali";
                  $v_Text[5] = "coperto";
                  $v_Text[6] = "";
                  $v_Text[7] = "nevoso con gelo";
                  $v_Text[8] = "";
                  $v_Text[9] = "";
                  $v_Text[10] = "";
                  $v_Text[11] = "piovigginoso";
                  $v_Text[12] = "piovoso";
                  $v_Text[13] = "Scattered Flurreis";
                  $v_Text[14] = "nevoso";
                  $v_Text[15] = "";
                  $v_Text[16] = "";
                  $v_Text[17] = "";
                  $v_Text[18] = "";
                  $v_Text[19] = "polveroso";
                  $v_Text[20] = "nebbioso";
                  $v_Text[21] = "foschioso";
                  $v_Text[22] = "fumoso";
                  $v_Text[23] = "ventoso";
                  $v_Text[24] = "coperto con vento";
                  $v_Text[25] = "estremamente freddo";
                  $v_Text[26] = "muvoloso";
                  $v_Text[27] = "molto nuvoloso";
                  $v_Text[28] = "molto nuvoloso";
                  $v_Text[29] = "parzialmente nuvoloso";
                  $v_Text[30] = "parzialmente nuvoloso";
                  $v_Text[31] = "parzialmente nuvoloso";
                  $v_Text[32] = "sereno";
                  $v_Text[33] = "sereno";
                  $v_Text[34] = "poco nuvoloso";
                  $v_Text[35] = "";
                  $v_Text[36] = "estremamente caldo";
                  $v_Text[37] = "con temporali isolati";
                  $v_Text[38] = "con temporali sparsi";
                  $v_Text[39] = "con rovesci di pioggia";
                  $v_Text[40] = "con rovesci di pioggia";
                  $v_Text[41] = "nevoso";
                  $v_Text[41] = "leggermente ventoso";
                  $v_Text[43] = "nevoso con vento";
                  $v_Text[44] = "parzialmente nuvoloso";
                  
                    while (!feof($fa))
                       {
                       $grabline = fgets($fa, 4096);
                       $grabline= trim($grabline) . "\n";
                       if (substr($grabline,7,4) == "City")    { $v_City    = substr($grabline,15,20); }
                       if (substr($grabline,7,6) == "SubDiv")  { $v_SubDiv  = substr($grabline,17,20); }
                       if (substr($grabline,7,7) == "Country") { $v_Country = substr($grabline,18,20); }
                       if (substr($grabline,7,6) == "Region")  { $v_Region  = substr($grabline,17,20); }
                       if (substr($grabline,7,5) == "Temp ")    { $v_Temp    = substr($grabline,15,20); }
                       if (substr($grabline,7,5) == "CIcon")   { $v_CIcon   = substr($grabline,16,20); }
                       if (substr($grabline,7,5) == "WindS")   { $v_WindS   = substr($grabline,16,20); }
                       if (substr($grabline,7,5) == "WindD")   { $v_WindD   = substr($grabline,16,20); }
                       if (substr($grabline,7,4) == "Baro")    { $v_Baro    = substr($grabline,15,20); }
                       if (substr($grabline,7,5) == "Humid")   { $v_Humid   = substr($grabline,16,20); }
                       if (substr($grabline,7,4) == "Real")    { $v_Real    = substr($grabline,15,20); }
                       if (substr($grabline,7,2) == "UV")      { $v_UV      = substr($grabline,13,20); }
                       if (substr($grabline,7,3) == "Vis")     { $v_Vis     = substr($grabline,14,20); }
                       if (substr($grabline,7,6) == "LastUp")  { $v_LastUp  = substr($grabline,17,200); }
                       if (substr($grabline,7,7) == "ConText") { $v_ConText = substr($grabline,16,20); } 
                  
                       if (substr($grabline,7,4) == "Fore")    { $v_Fore    = substr($grabline,15,200); }
                       if (substr($grabline,7,4) == "Acid")    { $v_Acid    = substr($grabline,15,20); }
                  //     print $grabline . "\n";
                       }
                  
                    $v_City    = substr($v_City,0,strlen($v_City)-3);
                    $v_SubDiv  = substr($v_SubDiv,0,strlen($v_SubDiv)-3);
                    $v_Country = substr($v_Country,0,strlen($v_Country)-3);
                    $v_Region  = substr($v_Region,0,strlen($v_Region)-3);
                    $v_Temp    = substr($v_Temp,0,strlen($v_Temp)-3);
                    $v_CIcon   = substr($v_CIcon,0,strlen($v_CIcon)-3);
                    $v_WindS   = substr($v_WindS,0,strlen($v_WindS)-3);
                    $v_WindD   = substr($v_WindD,0,strlen($v_WindD)-3);
                    $v_Baro    = substr($v_Baro,0,strlen($v_Baro)-3);
                    $v_Humid   = substr($v_Humid,0,strlen($v_Humid)-3);
                    $v_Real    = substr($v_Real,0,strlen($v_Real)-3);
                    $v_UV      = substr($v_UV,0,strlen($v_UV)-3);
                    $v_Vis     = substr($v_Vis,0,strlen($v_Vis)-3);
                    $v_LastUp  = substr($v_LastUp,0,strlen($v_LastUp)-3);
                    $v_Fore    = substr($v_Fore,0,strlen($v_Fore)-3);
                    $v_Acid    = substr($v_Acid,0,strlen($v_Acid)-3);
                  
                  $v_Fore = explode("|", $v_Fore);
                  
                  echo "<div id=\"weather\"><img src=\"immagini/weather_icons/".$v_CIcon . ".gif\" alt=\"Condizioni meteo\">";
                  
                  echo "A Trevignano il tempo è:<br />".$v_Text[$v_CIcon]."";
                  
                  
                  if ($tp == "C") {
                      echo " (";
                  	echo CelNumber($v_Temp);
                  	echo ")</div>";
                  } else {
                  	echo " $v_Temp°F";
                  }
                  
                  
                  ?>
                         <?
                  ?>
                        <?
                  fclose($fa);
                  }
                  ?>
                  
                        <?
                  //}
                  function CelNumber($number) {
                  	$number = $number-32;
                  	$number = $number * 5;
                  	$number = $number / 9;
                  	$number = round ($number);
                  	return $number."°C";
                  }
                      
                  function CelNumber2($number,$tp) {
                          if ($tp == "C") {                $number = $number-32;
                  	        $number = $number * 5;
                  	        $number = $number / 9;
                  	        $number = round ($number);
                  	        return "$number°C";
                          }       else {                return "$number°F";       } }
                  
                  
                  function PreNumber($number,$tp) {
                  	if ($tp == "C") {
                  		$number = $number *33.86;
                  		$number = round ($number);
                  		return "$number mbar";
                  	}
                  	else {
                  		return "$number inHg";
                  	}
                  }
                  
                  function LenNumber($number,$tp) {
                  	if ($tp == "C") {
                  		$number = $number * 1.852;
                  		$number = round ($number);
                          if ($number == 0)
                          {
                                  return "N/A";
                          }
                          else {
                  		return "$number km";
                  	}
                          }else {
                  	 if ($number == 0)
                             {
                  	return "N/A";
                             }
                  
                  
                  	else {
                  		return "$number mi";
                  	}
                  }
                     } 
                  
                  function VelNumber($number,$tp) {
                  	if ($tp == "C") {
                  		$number = $number * 0.44704;
                  		$number = round ($number);
                  		return "$number m/s";
                  	}
                  	else {
                  		return "$number mph";
                  	}
                  }
                  
                  function Fore($numbers) {
                  	if ($numbers == "1") {
                  		$date="DOMENICA";
                  		echo "$date";
                  	}
                  	elseif ($numbers == "2") {
                  		$date="LUNED&#204";
                  		echo "$date";
                  	}
                  	elseif ($numbers == "3") {
                  		$date="MARTED&#204";
                  		echo "$date";
                  	}
                  	elseif ($numbers == "4") {
                  		$date="MERCOLED&#204";
                  		echo "$date";
                  	}
                  	elseif ($numbers == "5") {
                  		$date="GIOVED&#204";
                  		echo "$date";
                  	}
                  	elseif ($numbers == "6") {
                  		$date="VENERDÌ";
                  		echo "$date";
                  	} else {
                  		$date="SABATO";
                  		echo "$date";
                  	}
                  }				
                  
                  # End of code
                  
                  switch($func) {
                  
                      default:
                      WeatherIndex($tp);
                      break;
                  }
                  
                  ?> 
                    • 10487 MODX Staff
                    • 1,535 Posts
                    Hi Bryn,

                    Please find attached a version of the script that should work as a snippet.

                    I’ve tested it on my website and the weather displays okay (I’m taking it that the weather icons are saved on your web server somewhere).

                    Have fun, Garry
                      Garry Nutting
                      Senior Developer
                      MODX, LLC

                      Email: [email protected]
                      Twitter: @garryn
                      Web: modx.com
                      • 4192
                      • 43 Posts
                      Wow! That was fast!  smiley
                      Thank you so much Garry for the edited PHP file. I have installed as a snippet and it seems working. However, I discovered that it doesn’t change the image nor the text description. Could it be a MODx cache problem? I have tested these two pages:

                      MODx test site (see the upper right)
                      Original PHP script

                      And with 3 different computers and 4 different browsers they show different icons/text, while they should be the same. The Original PHP page works 100% and shows updated data, while MODx shows always the same icon and data hour after hour... For some unknown reason it doesn’t update.

                      Do you have any idea of what’s wrong?

                      Thanks again for your help!