Domanda

Al momento, sto prendendo feed XML di un sito remoto e il salvataggio di una copia locale sul mio server da analizzare in PHP.

Il problema è come posso fare di aggiungere alcuni controlli in PHP per vedere se il file feed.xml è valido e in caso affermativo uso feed.xml.

E se non valido con errori (di cui a volte il feed XML remota Somes visualizzare Stai feed.xml vuoto), servire un backup valido copia del feed.xml da afferrare precedente / salvare?

codice afferrando feed.xml

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://domain.com/feed.xml');
/**
* Create a new file
*/
$fp = fopen('feed.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>

in modo da avere ora solo questo per caricarlo

$xml = @simplexml_load_file('feed.xml') or die("feed not loading");

grazie

È stato utile?

Soluzione

Se non è pricipial che ricciolo dovrebbe scrivere direttamente in un file, allora si potrebbe verificare XML prima di ri-scrivere il feed.xml locale:

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/feed.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec ($ch);
curl_close ($ch);
if (@simplexml_load_string($xml)) {
    /**
    * Create a new file
    */
    $fp = fopen('feed.xml', 'w');
    fwrite($fp, $xml);
    fclose($fp);
}

?>

Altri suggerimenti

Che ne dici di questo? Non c'è bisogno di usare curl, se avete solo bisogno di recuperare un documento.

$feed = simplexml_load_file('http://domain.com/feed.xml');

if ($feed)
{
    // $feed is valid, save it
    $feed->asXML('feed.xml');
}
elseif (file_exists('feed.xml'))
{
    // $feed is not valid, grab the last backup
    $feed = simplexml_load_file('feed.xml');
}
else
{
    die('No available feed');
}

In una classe ho messo insieme, ho una funzione che controlla se è presente il file remoto e se è di rispondere in modo tempestivo:

/**
* Check to see if remote feed exists and responding in a timely manner
*/
private function remote_file_exists($url) {
  $ret = false;
  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_NOBODY, true); // check the connection; return no content
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // timeout after 1 second
  curl_setopt($ch, CURLOPT_TIMEOUT, 2); // The maximum number of seconds to allow cURL functions to execute.
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');

  // do request
  $result = curl_exec($ch);

  // if request is successful
  if ($result === true) {
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($statusCode === 200) {
      $ret = true;
    }
  }
  curl_close($ch);

  return $ret;
}

La classe completo contiene il codice di ripiego per assicurarsi che abbiamo sempre qualcosa su cui lavorare.

Blog post che spiega la classe completo è qui: http: // weedygarden.net/2012/04/simple-feed-caching-with-php/

Codice è qui: https://github.com/erunyon/FeedCache

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