我正在使用drupal_http_request
来自另一个网站的xml字符串我正在尝试弄清楚如何将其作为xml抓取它似乎不起作用但是当我运行完全相同的网址时它以xml格式向我提供了有关如何执行此操作的任何想法的浏览器。如果我在执行$headers = array("Content-Type: text/xml")
时放入类似$http_contents = drupal_http_request($url, $headers = array("Content-Type: text/xml"));
的内容,它会以xml格式提供给我的数据但是它不是任何想法提前谢谢以下是我的代码
<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 />\n";
}
?>
<?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('&','&', $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 />\n";
echo "Display result " . $result . "<br />\n";
return $result;
}
?>
这是我收到的内容
LISTsamplelastname, samplefirstname samplemiddlenamesampleidsampleidstateDOB
当我通过浏览器运行网址时,我得到了这个
<?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>
在Supdley和我的同事John Salevetti的帮助下,我找到了解决方案。仅将xml内容包装在htmlspecialchars中,xml现在显示
答案 0 :(得分:0)
答案是在htmlspecialchars命令中包装有问题的xml内容,这将覆盖对页面上非html字符的抑制。想向Spudley发出一声呐喊,他看着这段代码,并提醒我一下nonhtml字符抑制感谢Spudley帮助我不要走太远那个兔子洞!
这是原来的......
echo "Display http_contents_data " . $http_contents_data . "<br />\n";
这是修改后的行......
echo "Display http_contents_data " . htmlspecialchars($http_contents_data) . "<br />\n";