Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 加载远程XML文件时出现问题_Php_Xml_Dom - Fatal编程技术网

Php 加载远程XML文件时出现问题

Php 加载远程XML文件时出现问题,php,xml,dom,Php,Xml,Dom,我正在尝试使用php加载一个远程xml文件 这是我的代码: $doc = new DOMDocument(); $doc->load($this->xml_file); $file = $doc->getElementsByTagName('file'); $totalFiles = $file->length; echo $totalFiles; 远程xml文件链接是: http://localhost/script/index.php?act=xml 这是index

我正在尝试使用php加载一个远程xml文件

这是我的代码:

$doc = new DOMDocument();
$doc->load($this->xml_file);
$file = $doc->getElementsByTagName('file');
$totalFiles = $file->length;
echo $totalFiles;
远程xml文件链接是:

http://localhost/script/index.php?act=xml

这是index.php中的代码:

但它仍然给我同样的错误。不过我注意到了一些事情,我尝试复制输出xml并将其保存在一个文件中,然后使用dom解析器读取它,结果是读取成功


但是当我试图解析php文件输出的xml时,它会抛出一个错误。

您的xml格式不正确。它有点毛病

尝试通过连接字符串来避免生成XML,因为这样会发生。您可以使用DomDocument创建XML,也可以读取和操作它

确保XML生成脚本中没有前导或尾随空格


您还应该使用CDATA

尝试调整输出?
$xml = '<?xml version="1.0"?><MultimediaGallery>';

$query = mysql_query('SELECT `id`,`image`,`desc` FROM `'
          .confitem('database','prefix').'table` ORDER BY `id` DESC LIMIT '
          .$start.','.$limit);
while($result = mysql_fetch_array($query))
{
    $img = unserialize($result['image']);
    $desc = unserialize($result['desc']);
    $xml .= '<file type="photo"><thumb>'
      .settings('site','url').'/'.OPATH_APPFOLDER.'/'
      .OPATH_UPFOLDER.'/wallpapers/thumbs/'.$img[0].'</thumb><source>'
      .settings('site','url')
      .'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER
      .'/wallpapers/'.$img[0].'</source><description>'
      .$desc[$_SESSION['languagecode']].'</description></file>';
}

$xml .= '</MultimediaGallery>';
header("content-type: text/xml");
echo $xml;
<?xml version="1.0"?><MultimediaGallery><file type="photo"><thumb>http://localhost/script/application/data/wallpapers/thumbs/1116205566_42ce0841ab_s.jpg</thumb><source>http://localhost/script/application/data/wallpapers/1116205566_42ce0841ab_s.jpg</source><description>dfdfdfdf</description></file></MultimediaGallery>
$xml = new DOMDocument('1.0');
$root  =  $xml->createElement('MultimediaGallery');
$xml->appendChild($root); 

$query = mysql_query('SELECT `id`,`image`,`desc` FROM `'.confitem('database','prefix').'backgrounds` ORDER BY `id` DESC LIMIT '.$start.','.$limit);
while($result = mysql_fetch_array($query))
 {
  $img   = unserialize($result['image']);
  $desc  = unserialize($result['desc']);
  $element = $xml->createElement('file');
  $root->appendChild($element);
  $attr  =  $xml->createAttribute('type');
  $element->appendChild($attr);
  $attr_text = $xml->createTextNode('photo');
  $attr->appendChild($attr_text);

  $thumb  = $xml->createElement('thumb');
  $element->appendChild($thumb);
  $thumb_text = $xml->createTextNode(settings('site','url').'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER.'/wallpapers/thumbs/'.$img[0]);
  $thumb->appendChild($thumb_text);

  $source  = $xml->createElement('source');
  $element->appendChild($source);
  $source_text = $xml->createTextNode(settings('site','url').'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER.'/wallpapers/'.$img[0]);
  $source->appendChild($source_text);

  $description  = $xml->createElement('description');
  $element->appendChild($description);
  $description_text = $xml->createTextNode($desc[$_SESSION['languagecode']]);
  $description->appendChild($description_text);
 }

header("content-type: text/xml");
echo $xml->saveXML();