الاستيلاء، ذاكرة التخزين المؤقت وفي التحليل تغذية XML عن بعد، وعمليات التحقق من الصحة في PHP

StackOverflow https://stackoverflow.com/questions/2262079

  •  20-09-2019
  •  | 
  •  

سؤال

وحاليا، أنا الاستيلاء على تغذية XML موقع بعيد وتوفير نسخة محلية في خدمة بلدي إلى أن تحليل في PHP.

والمشكلة هي كيف أذهب حول إضافة بعض الضوابط في PHP لمعرفة ما إذا كان ملف feed.xml صالحا وإذا كان الأمر كذلك استخدام feed.xml.

وإذا باطلة مع الأخطاء (والتي في بعض الأحيان تغذية XML النائية somes feed.xml عرض فارغة)، وخدمة نسخة صالحة احتياطية من feed.xml من انتزاع السابق / حفظ؟

وكود الاستيلاء 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);
?>

ودينا حتى الآن هذا فقط لتحميله

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

وذلك بفضل

هل كانت مفيدة؟

المحلول

وإذا لم يكن pricipial أن حليقة أن تكتب مباشرة في الملف، ثم هل يمكن أن تحقق XML قبل كتابة إعادة 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');
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);
}

?>

نصائح أخرى

وماذا عن هذا؟ لا حاجة لاستخدام حليقة إذا كنت بحاجة فقط إلى استرداد مستند.

$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');
}

في فئة أضع معا، ولدي وظيفة أن يتحقق في حالة وجود ملف عن بعد واذا كان الاستجابة في الوقت المناسب:

/**
* 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;
}

والطبقة كامل على رمز الاحتياطي للتأكد لدينا دائما شيء للعمل مع.

سجل آخر شرح الدرجة الكاملة هنا هو: HTTP: // weedygarden.net/2012/04/simple-feed-caching-with-php/

والرمز هنا هو: https://github.com/erunyon/FeedCache

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top