对API的XML查询返回“文件过早结束”

时间:2014-10-14 20:46:45

标签: php xml parsing

我正在尝试调用将返回XML响应的外部站点(API)。我已经尝试了几种方法来做到这一点并最终获得相同的响应:Premature end of file。很遗憾,我无法从其他网站获得任何积极的反馈。下面是我尝试过的php命令及其结果。在我看来,其他网站上存在问题。我可以复制URL并粘贴到浏览器中,它可以正常工作。

$url = "https://cli-cert.emdeon.com/servlet/XMLServlet?request=<?xml version='1.0'?>";
$url .= "<REQUEST userid='p_panda1' password='practice00' facility='3003154010'>";
$url .= "<OBJECT name='clinicalreport' op='search_filedelivery'>";
$url .= "<receivingorganization>3003154010</receivingorganization>";
$url .= "<creation_datetime_from>09/01/2014</creation_datetime_from>"; 
$url .= "<creation_datetime_to>10/10/2014</creation_datetime_to>";
$url .= "<is_downloaded>n</is_downloaded></OBJECT></REQUEST>";


$myXML = simplexml_load_file($url);
echo "<pre>";
print_r($myXML);

$postdata = file_get_contents($url);
echo "<pre>";
print_r($postdata);


$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

echo "<pre>";
echo $response,"<br>";

echo命令的输出:

SimpleXMLElement Object
(
    [0] => Premature end of file.
)
Premature end of file.
Premature end of file.

2 个答案:

答案 0 :(得分:0)

您创建的网址无效。

您正在创建以下网址:

https://cli-cert.emdeon.com/servlet/XMLServlet?request=<?xml version='1.0'?>...

这意味着你有2个问号。还例如&lt;据我所知,它不是一个有效的网址角色。

所以你必须对其进行url编码,例如:

$request = '<?xml version='1.0'?>...etc...';
$url = "https://cli-cert.emdeon.com/servlet/XMLServlet?request=". urlencode($request);

答案 1 :(得分:0)

您需要urlencode参数。查看http://php.net/manual/en/function.urlencode.php了解更多信息。将其改为此后,所有3人都会回复。

$url = "https://cli-cert.emdeon.com/servlet/XMLServlet?request=";
$xml = "<?xml version='1.0'?>";
$xml .= "<REQUEST userid='p_panda1' password='practice00' facility='3003154010'>";
$xml .= "<OBJECT name='clinicalreport' op='search_filedelivery'>";
$xml .= "<receivingorganization>3003154010</receivingorganization>";
$xml .= "<creation_datetime_from>09/01/2014</creation_datetime_from>"; 
$xml .= "<creation_datetime_to>10/10/2014</creation_datetime_to>";
$xml .= "<is_downloaded>n</is_downloaded></OBJECT></REQUEST>";

$url = $url . urlencode($xml);