2017-05-30 30 views
0

請道歉我的英文。如何驗證PHP標記是否存在來自外部服務器的PHP

我正在爲我的網站開發一種sitemap.xml系統,以將外部網站上的產品包含到我的數據庫中,就像谷歌對他的「蜘蛛」所做的那樣,我只是開發了整個腳本來驗證特定的站點地圖。 xml包含所有必需的標籤,它的工作原理:如果缺少任何標籤,腳本將返回錯誤,但是如果所有「產品」都具有所需的標籤,則腳本會返回成功消息。

但我只有一個問題:如果xml外部文件有任何錯誤,如缺少「關閉」標籤,腳本停止工作並返回錯誤。

我需要驗證外部xml文件是否包含任何錯誤,然後繼續執行我編寫的其餘代碼,如果結果爲「否」。

這是我開發的腳本:

<? 
$sitemap = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['sitemap']))))); 
$ref = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['ver_id']))))); 
    error_reporting(E_ALL); 
     ini_set('display_errors', '1'); 

     session_start(); 

     include dirname(dirname(__FILE__))."/connectdb.php"; 
    $query = "SELECT * FROM website WHERE `ver_id` = '$ref'"; 
    $result = $mysqli->query($query); 
    /* array asociativo */ 
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
    $url = $row['url']; 
    $long_url = $url.'/'.$sitemap; 
?> 
<?php 
$total = 0; 
$xml=simplexml_load_file("$long_url") or die("Error: Cannot create object"); 
$count = $xml->count(); 
foreach($xml->children() as $producto) { 
    $id = $producto->id; 
    $titulo = $producto->titulo; 
    $descripcion = $producto->descripcion; 
    $fotografia = $producto->fotografia; 
    $precio = $producto->precio; 
    $costo = $producto->costo; 
    $horario = $producto->horario; 
    $tiempo = $producto->tiempo; 
    $cobertura = $producto->cobertura; 
    $keywords = $producto->keywords; 
    $destacar = $producto->destacar; 
    $delivery = $producto->delivery; 

    if (!empty($id) && !empty($titulo) && !empty($descripcion) && !empty($fotografia) && !empty($precio) && !empty($costo) && !empty($horario) && !empty($tiempo) && !empty($cobertura) && !empty($keywords) && !empty($destacar) && !empty($delivery)) { 
     $total = $total + 1; } else { $total = $total - 1; }   
}  
     if($total < $count){ header("Location:/user/sitio.php?id=$ref&mensaje=1"); } else 
      if($total == $count){ 
      $query = "UPDATE website SET sitemap='$sitemap',sitemap_status='active' WHERE ver_id='$ref'"; 
      $result = $mysqli->query($query); 
      header("Location:/user/sitio.php?id=$ref&mensaje=2"); 
      } 
?> 

回答

0

用這個簡單的腳本解決了!

<? 
$dom = new DOMDocument; 
$dom->Load($long_url); 
if ($dom->validate()) { 
    echo "Valid File"; 
} else { echo "Invalid File"; } 
?> 

感謝大家。