[Linuxtrent] Re: Integrare Meteo Trentino in Wordpress

  • From: luca capra <luca.capra@xxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxx
  • Date: Wed, 03 Aug 2011 13:08:41 +0200

On 03/08/2011 11:51, Marco Creola wrote:
Ciao,
vi chiedo se qualcuno di voi ha mai utilizzato i dati XML del sito http://www.meteotrentino.it/ all'interno di un sito wordpress. Mi piacerebbe visualizzare le previsioni del tempo fornite dal sito di meteo trentino all'interno del mio sito ma non saprei da dove iniziare. Bisognerebbe fare un parsing dei dati XML ma non saprei come far eseguire l'eventuale codice PHP all'interno della home di Wordpress...
Qualcuno ha già affrontato il problema? Mi sapete aiutare in qualche modo?

I dati XML che mi interesserebbe gestire sono questi
->http://www.meteotrentino.it/bollettini/today/generale_it.xml

Grazie mille anticipatamente a chiunque possa darmi qualche indicazione utile!

Buona Giornata a tutti!


Io avevo usato questo qualche tempo fa.
L'unica nota è che usano UTF-16 e a me è servito convertirlo a utf8, poi puoi usare simplexml o altre librerie.
Non se funzioni ancora ma magari può servirti.

se metti in una pagina [meteo-tn] viene sostituito con quello che esce da get_meteotn()

Ciao, Luca

plugins/meteotn/meteotn.php<?php

/*

Plugin Name: meteo tn

Plugin URI:

Description: add [meteo-tn] substitution token to page content

Author:

Version: 0.1

Author URI:

*/

/*  Copyright 2008 Luca Capra luca.capra@xxxxxxxxx

    This program is free software; you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation; either version 2 of the License, or

    (at your option) any later version.

    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License

    along with this program; if not, write to the Free Software

    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die(); }

define('METEOTN_XML_BOLLETTINO', 
'http://www.meteotrentino.it/bollettini/today/generale_it');

function get_meteotn($url = null)

{

  if(is_null($url))

    $url = constant('METEOTN_XML_BOLLETTINO');

  $output='';

  try{

    $xmlstr = file_get_contents($url.'.xml');

    $xmlstr = _convert_to_utf8($xmlstr, 'UTF-16*');

    $xmlstr = trim($xmlstr, "\r\n");

    $xml = simplexml_load_string( $xmlstr );

    $img = "<img class='meteo-foto-oggi' src='" . (string)$xml->Oggi->imgtrentino . "' 
alt='previsioni per oggi' />";

    $output .= "{$img}<div 
class='evoluzioneTempo'>{$xml->EvoluzioneTempo}</div>";

  }

  catch(Exception $e)

  {

    $output.= 'Errore reperendo i data da Meteo TN: '.$e->getMessage();

  }

  return $output;

}

/**

  * @see 
http://api.drupal.org/api/drupal/includes--unicode.inc/function/drupal_convert_to_utf8/6

  */

function _convert_to_utf8($data, $encoding) {

  if (function_exists('iconv')) {

    $out = @iconv($encoding, 'utf-8', $data);

  }

  else if (function_exists('mb_convert_encoding')) {

    $out = @mb_convert_encoding($data, 'utf-8', $encoding);

  }

  else if (function_exists('recode_string')) {

    $out = @recode_string($encoding . '..utf-8', $data);

  }

  else {

//     watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode 
or mbstring for PHP.', array('%s' =>  $encoding), WATCHDOG_ERROR);

    throw new Exception(sprintf('Unsupported encoding %s. Please install iconv, 
GNU recode or mbstring for PHP.', $encoding));

    return FALSE;

  }

  return $out;

}

add_filter('the_content', 'f_content',16);

function f_content($content){

  global $post;

  $codes = array(

    "[meteo-tn]" =>  get_meteotn(),

  );

  foreach($codes as $find =>  $output ){

    $output= '';

    if(!(strstr($content, $find) === FALSE)){

      $content = str_ireplace($find, $output, $content);

    }

  }// foreach

  return $content;

}

Other related posts: