We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16732
    • 592 Posts
    @jesster444 : Yes here my rapid solution :

    
    <?php
    // Resource Name: WeatherX
    // Author: Jesse Rochman
    // Forum Alias: jesster444
    // Version: 1.0
    // Compatible with .95Beta5
    // Released: 11/1/2006
    // Created from Nick Schaffner's WEATHER.COM XML PARSER
    // Template Placeholders: wxIcon, wxSunr, wxSuns, wxWindSpeed, wxWindir, wxTemp, wxConditions, wxDate
    
    //////// Some Constants
    $wxUrl	 = 'http://xoap.weather.com/weather/local/';	// URL to xoap.weather
    $wxUPDATE_CONSTANT = 100; //This is how often the weather will be updated.  DON'T CHANGE!
    $wxNow = time(); //This allows us to see if we need to update variables
    
    //////// Set Varibles Below
    $wxZipcode = 'FRXX0016';				// Zipcode  Bordeaux, FR
    $wxPartner = 'xxxxx';			// Partner ID
    $wxLicense = 'xxxxxx';		// License Key
    $wxIcons = 'assets/snippets/weatherx/graphics';			// Path to Weather Icons
    $wxUnits = 'metric';				// Set to 'english' or 'metric' (Celsius & KMH)
    $wxClock = 24;		// Set to '12' or '24' hour format
    $wxUpdateFile = $modx->config['base_path'] . 'assets/snippets/weatherx/weather.txt';
    
    /////Read the file and establish all the output variables
    $handle = fopen($wxUpdateFile, 'r');
    $contents = fread($handle, filesize($wxUpdateFile));
    fclose($handle);
    $contents = explode('|', $contents);
    
    $wxLastUpdate = $contents[0] + $wxUPDATE_CONSTANT;
    $wxIcon = $contents[1];
    $sunr = $contents[2];
    $suns = $contents[3];
    $date = $contents[4];
    $s = $contents[5];
    $conditions = $contents[6];
    $tmp = $contents[7];
    $windir = $contents[8];
    
    $getXmlMethod = (isset($getXmlMethod))? $getXmlMethod : '';
    /////Set all the placeholders so you can output the format in a template or chunk!
    $modx->setPlaceholder('wxIcon', $wxIcon);
    $modx->setPlaceholder('wxSunr', $sunr);
    $modx->setPlaceholder('wxSuns', $suns);
    $modx->setPlaceholder('wxDate', $date);
    $modx->setPlaceholder('wxWindSpeed', $s);
    $modx->setPlaceholder('wxConditions', $conditions);
    $modx->setPlaceholder('wxTemp', $tmp);
    $modx->setPlaceholder('wxWindDir', $windir);
    
    //Check to see if we need to update the variables otherwise we are done.
    if ($wxLastUpdate < $wxNow) {
    
    	//////// Parse XML
    	$file = $wxUrl . $wxZipcode . '?cc=*&&prod=xoap&par=' . $wxPartner. '&key=' . $wxLicense;
    
    	$xml_parser = xml_parser_create();
    
    	     if (file_exists($file)){
    	            $data = implode('', file($file));
    	         } else {
    
                    if($getXmlMethod = 'curl'){
    
                         function get_xml($url, $parametres=array()) {
                              $params='';
                              if ($parametres['Referer']!="") $params.='-e '.$parametres['Referer'];
                              if ($parametres['Proxy']!="") $params.='-x '.$parametres['Proxy'];
                              if ($parametres['BrowserName']!="") $params.='-A "'.$parametres['BrowserName'].'"';
                              return (`curl $params $url`);
                          }
                          $data = get_xml($file);
                    }
                    else {
                              $fp = fopen($file,'r');
            	            while(!feof($fp)){
            	               $data = $data . fread($fp, 1024);
            	            }
            	            fclose($fp);
                     }
    
    
    
    		}
    
    	xml_parse_into_struct( $xml_parser, $data, $vals, $index );
    	xml_parser_free( $xml_parser );
    
    
    	$t = true;
    
    	foreach ($vals as $key => $i) {
    
    	if ($vals[$key]['value'] != false) {
    
    		if ($vals[$key]['tag'] != 'T') {
    
    			$temp = strtolower($vals[$key]['tag']);
    			$$temp = $vals[$key]['value'];
    
    		} elseif ($t == true) {
    
    			if ($vals[$key]['level'] == 3) $conditions = $vals[$key]['value'];
    			else { $windir = $vals[$key]['value']; $t = false; break; }
    
    		}
    	}
    
    	}
    
    
    	//////// Make sense of the varibles
    
    	$sunr = str_replace (' ','',$sunr);
    	$suns = str_replace (' ','',$suns);
    	$date = date('d/m/Y');
    
        //$Ville= obst
    
    	if ($wxUnits == 'metric') { // Set Metric
    		$tmp = round((($tmp - 32)/9) * 5);
    		$flik = round((($flik - 32)/9) * 5);
    		$s = round($s * 1.609);
    	}
    
    	if ($wxClock == 24)  {
    		$sunr = date('H:i',strtotime($sunr));
    		$suns = date('H:i',strtotime($suns));
    
    	}
    
    
    	if ($s == 'calm') $wind = 'Calm';
    
    	$wxIcon = $modx->config['base_url'] . $wxIcons . '/' . $icon . '.png';
    
    	$wxWrittenInfo = $wxNow . '|' . $wxIcon . '|' . $sunr . '|' .$suns . '|' . $date . '|' . $s . '|' . $conditions . '|' . $tmp . '|' . $windir;
    
    	// Let's make sure the file exists and is writable first.
    	if (is_writable($wxUpdateFile)) {
    
    
    	   if (!$handle = fopen($wxUpdateFile, 'w')) {
    	         exit;
    	   }
    
    	   // Write content to our opened file.
    	   if (fwrite($handle, $wxWrittenInfo) === FALSE) {
    	       exit;
    	   }
    
    	   fclose($handle);
    
    	}
    
    }
    ?>
    


    to use with :

    [!WeatherX getXmlMethod=`curl`!]
    


    wink
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      What happens if the server "http://xoap.weather.com/weather/local/" is not available?
        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
        • 14050
        • 788 Posts
        Susan,

        The world will most likely blow up. All kidding aside, nice catch. It will definitely crash the snippet and throw out an exception. I tried to do a check for this but am having a little trouble. fopen() is supposed to return ’FALSE’ if it fails and toss out an exception, which MODx catches and gives an error message about. So I tried doing a little test where I put
        if (fopen($file))
        which doesn’t work because of the exception tossed. I also tried
        if (@fopen($file))
        which supposedly should suppress the exception and just return false, but still MODx gives me an error. I also tried using file() and file_get_contents(), but those all use fopen(), I believe. So any ideas on how to check to make sure the feed is working? I think this should also be implemented in my GoogleEvents feed too!
          Jesse R.
          Consider trying something new and extraordinary.
          Illinois Wine

          Have you considered donating to MODx lately?
          Donate now. Every contribution helps.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          MODx catches the low-level library error, which the user error handling never sees. I gave it up, and used curl. Actually, I have curl upload to a file, and then have an xml parser to parse the really ugly xml this weather feed I’m using passes out. If curl can’t access the URL you give it, it will just load an empty file. Then my xml parser checks for a file with filesize of 0, and displays a nice message. The only drawback is that not all php installations have it.
          $xml1 = "http://www.the.url.com";
          $file1 = "assets/snippets/weather/file/weather.xml";
          $sh1 = curl_init( $xml1 );
          $fh1 = fopen( $file1, 'w' );
          curl_setopt($sh1, CURLOPT_HEADER, 0);
          curl_setopt($sh1, CURLOPT_FAILONERROR, 1);
          curl_setopt( $sh1, CURLOPT_FILE, $fh1 );
          curl_exec($sh1);
          curl_close(  $sh1 );
          fclose(  $fh1 );
          if(file_exists($file1) && filesize($file1) != 0) {
          ... etc...
          } else {
            echo "Sorry, no weather today.";
          }
          

          You could also check the status values curl returns, it returns the html_code and you can check for 200.
            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
            • 14050
            • 788 Posts
            So the problem I am looking at is that if CURL is not part of the PHP installation, then I would want to try and fall back to the fopen of the URL directly, probably by allowing the user to pass a parameter picking the method they want. However, if the site is down this would crash the snippet. Is there anyway to check if a the site is down without using CURL and using something that is part of every or almost every PHP install that won’t crash the snippet by sending out a low_level library error?
              Jesse R.
              Consider trying something new and extraordinary.
              Illinois Wine

              Have you considered donating to MODx lately?
              Donate now. Every contribution helps.
              • 15987
              • 786 Posts
              could you just put the fopen inside of a try/catch block? Maybe that would get the error instead of it going to modx? I haven’t tried this but it might work??
                • 14050
                • 788 Posts
                Kyle,

                That is an interesting idea. I remember doing similar things in Java. A quick search though, makes me believe that functionality is only available to people with PHP 5. Unfortunately, I run all my stuff on a shared server and they are still using PHP 4.3.11. I imagine once everyone is migrated over to PHP 5, that this will be an ideal solution. That way I could try using fopen, catch the errors if it doesn’t work, try curl, etc. all without user interaction.
                  Jesse R.
                  Consider trying something new and extraordinary.
                  Illinois Wine

                  Have you considered donating to MODx lately?
                  Donate now. Every contribution helps.
                  • 14050
                  • 788 Posts
                  Alright everyone. I have released the second beta of WeatherX. I would really recommend using this method as it does some additional checks to make sure it won’t crash. The first post of the page contains the file attached and the updated info.
                    Jesse R.
                    Consider trying something new and extraordinary.
                    Illinois Wine

                    Have you considered donating to MODx lately?
                    Donate now. Every contribution helps.
                    • 1226
                    • 5 Posts
                    I receive the following output in a div I call the snippet. I’m using wunderground:

                    Parse error: syntax error, unexpected '<' in /home/.patricia/saddletramp/saddletramps.org/manager/includes/document.parser.class.inc.php(705) : eval()'d code on line 1


                    Any ideas?
                      • 1226
                      • 5 Posts
                      I figured out why. The php file has <?php ?> open and closing tags. Removing those fixed the problem. Just an FYI.

                      It’s working great now.

                      Also, would could you add in the functionality of having custom dates? something like
                      &wxDate=`Day, Month, Year`
                      would be much appreciated.