Grab, cache et l'analyse du flux XML à distance, les contrôles de validation en PHP
-
20-09-2019 - |
Question
À l'heure actuelle, je suis preneuse un feed XML de site distant et enregistrer une copie locale sur mon serveur pour être analysé en PHP.
Le problème est comment dois-je ajouter quelques vérifications à propos de PHP pour voir si le fichier flux.xml est valide et si l'on utilise de manière flux.xml.
Et si invalide avec des erreurs (dont parfois le flux XML à distance affichent Somes flux.xml), en signifier une copie de sauvegarde valide du flux.xml de grab précédent / sauver?
Code accaparement flux.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);
?>
jusqu'à présent que cette pour le charger
$xml = @simplexml_load_file('feed.xml') or die("feed not loading");
Merci
La solution
Si ce n'est pas pricipial que boucle devrait écrire directement dans le fichier, vous pouvez vérifier XML avant de re-écrire votre flux.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);
}
?>
Autres conseils
Comment cela? Pas besoin d'utiliser Ondulations Si vous avez juste besoin de récupérer un document.
$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');
}
Dans une classe, je mets ensemble, j'ai une fonction qui vérifie si le fichier existe à distance et si elle est de répondre en temps opportun:
/**
* 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 complète contient le code de repli pour nous assurer que nous avons toujours quelque chose à travailler avec.
Blog post expliquant la classe complète est ici: http: // weedygarden.net/2012/04/simple-feed-caching-with-php/
Le code est ici: https://github.com/erunyon/FeedCache