1

我想在 php 中加载远程 xml 文件并将其转换为 Json。首先我使用 cUrl 来获取 xml 数据:

xmlparse.php

header('Content-Type: text/xml');
$url = 'http://www.example.com/xml.php';
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_COOKIE,'Esperantus_Language_ime=en-US;Esperantus_Language_fa=en-US;PortalAlias=fa;refreshed=true;.ASPXAUTH=;portalrole=;');
curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_exec($curl); 

执行脚本 xml 时显示。

我使用以下函数将 xml 转换为 json :

索引.php

function Parse ($url) {
  $fileContents= file_get_contents($url);

  //$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
  //$fileContents = trim(str_replace('"', "'", $fileContents));

  $simpleXml = simplexml_load_string($fileContents);
  $json = json_encode($simpleXml);

  return $json;
}
echo Parse('xmlparse.php');

但这不起作用并出现错误:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 8: parser error : ParsePI: PI php never end ... in D:\xampp\htdocs\xml\index.php on line 12
Warning: simplexml_load_string() [function.simplexml-load-string]: curl_exec($curl); in D:\xampp\htdocs\xml\index.php on line 12
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\xampp\htdocs\xml\index.php on line 12
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 8: parser error : Start tag expected, '<' not found in D:\xampp\htdocs\xml\index.php on line 12
Warning: simplexml_load_string() [function.simplexml-load-string]: curl_exec($curl); in D:\xampp\htdocs\xml\index.php on line 12
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\xampp\htdocs\xml\index.php on line 12
false

如何加载远程 xml 并转换为 json。

4

0 回答 0