XML有时会返回false?

时间:2016-10-13 06:50:40

标签: php xml

我有这个代码从文件中检索XML(这是index.php的完整代码):

  <?php
  libxml_use_internal_errors(true);
  if(isset($_GET['user']))$user=htmlentities($_GET['user']);else $user="";
  if(isset($_GET['designer']))$designer=htmlentities($_GET['designer']);else $designer="";
  if(isset($_GET['id'])){$id = $_GET['id'];}else{if(isset($_SESSION['user'])){$id=$_SESSION['user'];}else{$id="";}}
  libxml_use_internal_errors(true);
  $form = '...form goes here...';
  if(isset($_GET['user']) && isset($_GET['designer']) && isset($_GET['id'])){
    if(empty($_GET['user']) or empty($_GET['designer']) or empty($_GET['id'])){
        echo '<div class="error">Please fill out all fields!</div>';
        echo $form;
    } else if($_GET['id']<0 or !is_numeric($_GET['id']) or !is_numeric($_GET['user'])){
        echo '<div class="error"><b>Invalid user ID:</b> Only numeric values allowed</div>';
        echo $form;
    } else {
        $_SESSION['user'] = $_GET['id'];
        $lop = curl_init($uopxls);
        curl_setopt($lop, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($lop, CURLOPT_HTTPHEADER, array("Cookie: pdhUser=19982"));
        $getMedItemsFile = curl_exec ($lop);
                    $xml = @simplexml_load_string(trim($getMedItemsFile), "SimpleXMLElement", LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING);
                    if($xml===FALSE){
                        echo '<div class="error"><b>Something weird happened.</b> Please reload the page or contact me for help. If reloading the page once doesn\'t help, keep trying to reload.</div>';
                        foreach(libxml_get_errors() as $error) {
                          echo "\t", $error->message;
                        }
                    } else {
                        $i=0;
                        foreach($xml->shopItems->item as $item){
                          if(strcasecmp($item['name'], 'Designed by '.$designer.'') == 0 && $item['brand']=='555'){
                              $i++;
                              $curl = curl_init();
                              curl_setopt($curl, CURLOPT_URL, "http://www.stardoll.com/en/ajax/reports/getDataForReport.php");
                              curl_setopt($curl, CURLOPT_POST, 1);
                              curl_setopt($curl, CURLOPT_POSTFIELDS, "reportedUserId=&customItemId=".$item['customItemId']."");
                              curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: pdhUser=19982"));
                              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                              curl_setopt($curl, CURLOPT_ENCODING,  '');
                              curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,0); 
                              curl_setopt($curl, CURLOPT_TIMEOUT, 400);
                              $itemImage=curl_exec ($curl);
                              ob_flush();
                              $imgss=json_decode($itemImage,true);
                              curl_close ($curl);
                            ?>
                            ...here some content...
                            <?php
                        }
                      }
                      $foundItemsMsg="<p>This user has ".count($xml->shopItems->item)." item(s) in total and ".count($xml->shopItems->item['type']=='HAIR')." wigs in their beauty parlor. We found ".$i." wig(s) designed by <b>$designer</b>. <a href=\"/\">Clone other wigs ></a></p>";
                      echo $foundItemsMsg;
                    }
    }
  } else {
      /* If $_GET's are not set */
    echo $form;
  }
  ?>

有时这会回应“没有工作!”,但是当我重新加载页面时它有效(有时我必须重新加载两次)。其他时候它正常工作。

有人可以帮我弄清楚它为什么会这样做,以及如何阻止它再次这样做?

谢谢:)

1 个答案:

答案 0 :(得分:1)

可能存在间歇性的解析错误或网络故障。 simplexml_load_string的手册指出:

  

错误/异常

     

为XML数据中发现的每个错误生成E_WARNING错误消息。   提示

     

使用libxml_use_internal_errors()来抑制所有XML错误,然后使用libxml_get_errors()来迭代它们。

为了帮助解决此问题,请使用libxml_get_errors()函数返回在解析XML文件期间遇到的错误数组。

PHP手册中的代码示例:

   if ($xml === FALSE) {
       $errors = libxml_get_errors();

       foreach ($errors as $error) {
           echo display_xml_error($error, $xml);
       }

       libxml_clear_errors();
   }

显示错误的示例

   function display_xml_error($error, $xml)     {
       $return  = $xml[$error->line - 1] . "\n";
       $return .= str_repeat('-', $error->column) . "^\n";

       switch ($error->level) {
           case LIBXML_ERR_WARNING:
               $return .= "Warning $error->code: ";
               break;
            case LIBXML_ERR_ERROR:
               $return .= "Error $error->code: ";
               break;
           case LIBXML_ERR_FATAL:
               $return .= "Fatal Error $error->code: ";
               break;
       }

       $return .= trim($error->message) .
                  "\n  Line: $error->line" .
                  "\n  Column: $error->column";

       if ($error->file) {
           $return .= "\n  File: $error->file";
       }

       return "$return\n\n--------------------------------------------\n\n";
   }