du1068
du1068
2013-09-04 20:16

我的php不呈现xml输出; 但是在浏览器上正确呈现

  • xml
  • php

I am using a drupal_http_request to an xml string from another web site I am currently trying to figure out how to grab this as an xml it does not seem to be working but when I run the exact same url in a browser it gives me back the information in xml format any ideas on how to do this Thought if I put something in like $headers = array("Content-Type: text/xml") when I execute $http_contents = drupal_http_request($url, $headers = array("Content-Type: text/xml")); it would give me the data in an xml format but it is not any ideas thanks in advance Below is my code

<form method="post">
<p>Last Name: <input type="text" name="lastname" /><br />
First Name: <input type="text" name="firstname" /></p>
<p><input type="submit" value="Send it!"></p>
</form>

<?php

   if($_POST)
   {


      $url = "https://pdb-services-beta.nipr.com/pdb-xml-reports/hitlist_xml.cgi?customer_number=testlogin&pin_number=testpin&report_type=1";
      $url = $url . "&name_last=" . $_POST['lastname'] ."&name_first=". $_POST['firstname'];

      $result = grabData($url);
      $xml=simplexml_load_file("$result.xml");
      $nipr_id = $xml->NPN;

      echo "Agent  " . $_POST['firstname'] . " " . $_POST['lastname'] . " Id is:".  $nipr_id  . "<br />
";
   }
?>

<?php

function grabData($url)
{

$http_contents = drupal_http_request($url);
    if (!isset($http_contents->data)) {
        throw new RuntimeException("Cannot get contents from the URL");
    }

    if($replace_special_characters)
        $http_contents_data = str_replace('&','&amp;', $http_contents->data);
    else
        $http_contents_data = $http_contents->data;

    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $http_contents_data, $result);
    xml_parser_free($xml_parser);
    echo "Display http_contents_data " . $http_contents_data .  "<br />
";
    echo "Display result " . $result .  "<br />
";
    return $result;

}

?>

here is what I am receiving

LISTsamplelastname, samplefirstname samplemiddlenamesampleidsampleidstateDOB 

and when I run the url through the browser I get this

<?xml version='1.0' encoding='UTF-8'?>
<HITLIST> 
   <TRANSACTION_TYPE> 
     <TYPE>
         LIST
     </TYPE> 
   </TRANSACTION_TYPE> 
   <INDIVIDUAL>
        <NAME>
             samplelastname, samplefirstname samplemiddlename
        </NAME>      
        <ID_ENTITY>
             sampleid
        </ID_ENTITY>
        <NPN>
            sampleid
        </NPN>
        <STATE_‌​RESIDENT>
         state
        </STATE_RESIDENT>
        <DATE_BIRTH>
         dob
        </DATE_BIRTH>
  </INDIVIDUAL> 
  <INDIVIDUAL>
  </INDIVIDUAL> 

With the help of Supdley and My fellow worker John Salevetti I found the solution. Merely wrap the xml contents in htmlspecialchars the xml now displays

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答