出于某种原因,此操作为我提供了“无效的返回格式”。
$dom = new DomDocument('1.0', 'utf-8');
$ret = @$dom->loadXML($data);
if (! $ret) {
$_SESSION['error'] = 'invalid returned format';
return false;
}
我使用$ data = curl_exec($ socket)来检索XML文件的链接,并且我能够使用此操作回显文件的内容类型,因此该文件存在:
if (strpos($content_type, 'text/xml') === FALSE) {
$_SESSION['error'] = 'invalid returned format';
return false;
}
我怎样才能让它不是错误?
感谢!!!
额外代码:
curl_setopt($socket, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($socket, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($socket, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($socket, CURLOPT_TIMEOUT, (15+5));
curl_setopt($socket, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($socket);
$code = curl_getinfo($socket, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($socket, CURLINFO_CONTENT_TYPE);
curl_close($socket);
if ($code != 200) {
$_SESSION['error'] = 'Unable to talk to sm';
return false;
}
if (strpos($content_type, 'text/xml') === FALSE) {
$_SESSION['error'] = 'invalid returned format';
return false;
}
$dom = new DomDocument('1.0', 'utf-8');
$ret = @$dom->loadXML($data);
if (! $ret) {
$_SESSION['error'] = 'invalid returned format';
return false;
}
if ($dom->documentElement->nodeName != 'user') {
$_SESSION['error'] = 'authentication issue';
return false;
}
XML CODE:
<?xml version="1.0" encoding="UTF-8"?>
<user login="myfile">
<application id="10" name="test" description="test"> … </application>
</user>
XML RENDER FILE
$applications = $user->applications();
header('Content-Type: text/xml; charset=utf-8');
$dom = new DomDocument('1.0', 'utf-8');
$user_node = $dom->createElement('user');
$user_node->setAttribute('login', $user->getAttribute('login'));
foreach ($applications as $application) {
$application_node = $dom->createElement('application');
$application_node->setAttribute('id', $application->getAttribute('id'));
$application_node->setAttribute('name', $application->getAttribute('name'));
$application_node->setAttribute('description', $application->getAttribute('description'));
foreach ($application->getMimeTypes() as $mimetype) {
$mimetype_node = $dom->createElement('mime');
$mimetype_node->setAttribute('type', $mimetype);
$application_node->appendChild($mimetype_node);
}
$user_node->appendChild($application_node);
}
$dom->appendChild($user_node);
echo $dom->saveXML();
exit(0);
答案 0 :(得分:0)
最有可能application/xml
而非text/xml
(除非您手动将标头设置为该标头或服务器,因为某些奇怪的原因,它与text/xml
相关联。)
如果不是这种情况,则您的问题是您尝试访问的文件的xml格式无效。使用一些在线XML验证器进行检查,或者在Chrome或Firefox中将其用药,它应该显示synax错误。
对未来的一些建议:
$content_type
变量 - 如果有,请自行调试几分钟?希望有所帮助。