0

I have this code to retrieve XML from a file (this is the whole code of 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;
  }
  ?>

Sometimes this echoes "Did not work!", but when I reload the page it works (sometimes I have to reload twice). Other times it works normally.

Can someone help me figure out why it's doing this and how to prevent it from doing it again?

Thanks :)

LW001
  • 2,452
  • 6
  • 27
  • 36
Jade Kallo
  • 110
  • 1
  • 10

1 Answers1

1

There could be intermittent parsing errors, or network failures. The manual for simplexml_load_string states:

Errors/Exceptions

Produces an E_WARNING error message for each error found in the XML data. Tip

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

To help trouble shoot this, use the libxml_get_errors() function to return an array of errors that were encountered during the parsing of the XML file.

A code example from the PHP manual:

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

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

       libxml_clear_errors();
   }

And an example for displaying the 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";
   }
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Hey thanks for your answer. I tried that but it wouldn't display any errors, it would just display "Did not work!". Do you know if there is any way to ignore XML errors and attempt to continue (other than `LIBXML_ERR_NONE` and `LIBXML_NOERROR`)? Thanks :) – Jade Kallo Oct 13 '16 at 13:17
  • @JadeKallo: Can you update your question with the code you used? – Greg Burghardt Oct 13 '16 at 14:30