Domanda

La mia homepage contiene tempo per tre città in tutto il mondo come mostrato nell'immagine

index.php

Nella home page dichiaro 3 variabili che immagazzinano l'URL RSS per ogni città

$newYorkWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f';
$londonWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c';
$parisWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c';

Tiro i tag identici dai tre URL visualizzati sopra e uso 3 funzioni identiche a parte la variabile passata al suo interno.

Di seguito mostra la variabile passata nella funzione. Ovviamente vengono utilizzate altre funzioni prima che possano essere restituiti $ Weather.

function new_york_current_weather($newYorkWeatherSource) {

// Get XML data from source
if (isset($newYorkWeatherSource)) {
    $feed = file_get_contents($newYorkWeatherSource);
} else {
    echo 'Feed not found.  Check URL';
}

checkWeatherFeedExists($feed);
$xml = new SimpleXmlElement($feed);

$weather = get_dateTime($xml);
$weather = get_temperature_and_convert($xml);
$weather = get_conditions($xml);
$weather = get_icon($xml);

return $weather;
}

Come ho già detto, ripeto che ripeto questa funzione 3 volte solo sostituendo la variabile $ NewYorkWeatheRersource che viene passata nell'esempio sopra. Qualche idea su come potrei riutilizzare questa funzione 3 volte ma passare in un URL diverso per mantenere la mia homepage mostrando il tempo dalle 3 città? Ovviamente, è facile riutilizzare la funzione se ogni città era rappresentata su singole pagine, ma lo scopo è tenerle insieme per il confronto.

Qualche idea?

Grazie in anticipo.

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top