出于某种原因,此操作给了我一个“无效的返回格式”。
$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 代码:
<?xml version="1.0" encoding="UTF-8"?>
<user login="myfile">
<application id="10" name="test" description="test"> … </application>
</user>
XML 渲染文件
$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);