[WordPress] problemi plug meteo api key

michele81

Nuovo Utente
2 Dic 2019
4
0
1
Salve ha tutti devo sviluppare un plugin che crei un widget meteo sto usando come servizio di apikey : https://www.wunderground.com/member/api-keys questo è il mio script non funziona :-( potete aiutarmi per favore?
Il plug in dovrebbe fungere cosi' :
- creo un widget da inserire in side bar
- do la possibilità all utente di inserire Titolo / citta / apikey
- mi dovrebbe ritornare la temperatura etc etc della citta richiesta

non succede niente sembra come se l apikey non funzionasse
PHP:
<?php
/*
Plugin Name: meteo plugin
Plugin URI: ###
Description:
Version: 1.0
Author:
Author URI:
License: GPL2
*/

class Wunderground_Widget extends WP_Widget {

public function __construct() {
parent::__construct(
'wunderground_widget',
'Wunderground Widget',
array( 'description' => 'A Widget for displaying the current weather using the Wunderground API' )
);
}

public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$api_key = $instance['api_key'];
$location = $instance['location'];

wp_register_style('meteo-style', plugins_url('meteo/meteo.css', dirname(__FILE__)));
wp_enqueue_style('meteo-style');

$api_url = 'http://api.wunderground.com/api/'.$api_key.'/conditions/forecast/q/'.$location.'.json';
$response = wp_remote_get( $api_url );

$wunderground = json_decode($response['body']);

echo $before_widget;
?>
<div class='wunderground-widget'>
<?php if(isset($title)){ ?>
<h3 class='widget-title'><?php echo $title ?></h3>
<?php
}

$current = $wunderground->{'current_observation'};
$location = $current->{'display_location'}->{'full'};
$temp = $current->{'feelslike_c'};
$wind = $current->{'wind_kph'};
$wind_dir = $current->{'wind_dir'};
$humidity = $current->{'relative_humidity'};
$summary = $current->{'weather'};

$forecast = $wunderground->{'forecast'}->{'txt_forecast'}->{'forecastday'}[0];
$icon = $forecast->{'icon'};
$icon_url = $forecast->{'icon_url'};
$text = $forecast->{'fcttext_metric'};
?>
<div>
<div class="temp"><?php echo $temp ?>&deg;C <img src="<?php echo $icon_url ?>" alt="<?php echo $icon ?>"/></div>
<div class="location"><?php echo $location ?></div>
<div class="wind">Vento: <?php echo $wind ?>kph (<?php echo $wind_dir ?>)</div>
<div class="humidity">Umido': <?php echo $humidity ?></div>
<div class="summary" title="<?php echo $text ?>">Sunto: <?php echo $summary ?></div>
</div>
</div>
<?php
echo $after_widget;
}

public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['api_key'] = strip_tags( $new_instance['api_key'] );
$instance['location'] = strip_tags( $new_instance['location'] );
return $instance;
}

public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = 'New title';
}

if ( isset( $instance[ 'api_key' ] ) ) {
$api_key = $instance[ 'api_key' ];
}
else {
$api_key = '';
}

if ( isset( $instance[ 'location' ] ) ) {
$location = $instance[ 'location' ];
}
else {
$location = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'location' ); ?>"><?php _e( 'Location:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'location' ); ?>" name="<?php echo $this->get_field_name( 'location' ); ?>" type="text" value="<?php echo esc_attr( $location ); ?>" />

<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />

<label for="<?php echo $this->get_field_id( 'api_key' ); ?>"><?php _e( 'API Key:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'api_key' ); ?>" name="<?php echo $this->get_field_name( 'api_key' ); ?>" type="text" value="<?php echo esc_attr( $api_key ); ?>" />
</p>
<?php
}

}

function wunderground_widgets_init(){
register_widget( 'Wunderground_Widget' );
}
add_action( 'widgets_init', 'wunderground_widgets_init' );
 
Ultima modifica di un moderatore:
@michele81
Da regolamento del forum, come tutti noi sei tenuto ad usare il tag
code-gif.6007
o il tag
php-png.6009
per il PHP, quando posti del codice, oppure la funzione codice dalla barra degli strumenti
box-inserisci-2-png-jpg.6008


Inoltre ti prego di leggere attentamente il regolamento generale del forum e quello di sezione dove posti prima di continuare
Grazie

Per questa volta te lo sistemo io ma mi raccomando per il futuro
 
Oltre ai tag non devi aprire due discussioni uguali ne nella stessa sezione ne in sezioni diverse
 

Discussioni simili