du1068 2013-09-04 20:16
浏览 72
已采纳

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

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条回答 默认 最新

  • douao8353 2013-09-06 18:55
    关注

    The answer to this was wrapping the xml content in question in a htmlspecialchars command this will override the suppression of non-html characters on the page. Would like to send a shoutout to Spudley who looked at this code and reminded me of the nonhtml character suppression Thanks Spudley for helping me not to go too far down that rabbit hole!

    this was the original line ....

    echo "Display http_contents_data " . $http_contents_data .  "<br />
    ";
    

    and this is the modified line......

    echo "Display http_contents_data " . htmlspecialchars($http_contents_data) .  "<br />
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求