将php生成的xml转换为json

将php生成的xml转换为json,php,xml,json,Php,Xml,Json,我想用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-U

我想用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:

index.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
警告:simplexml\u load\u string()。。。在第12行的D:\xampp\htdocs\xml\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:curl\u exec($curl);在第12行的D:\xampp\htdocs\xml\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^位于第12行的D:\xampp\htdocs\xml\index.php中

警告:simplexml\u load\u string()[函数.simplexml加载字符串]:实体:第8行:解析器错误:应为开始标记,“什么具体不起作用?”?您是否尝试过删除所有的
str\u replace()
s?如果XML是有效的,那么它们看起来是不必要的解析器抱怨的行是什么?XML看起来像什么?在将其传递给
simplexml\u load\u string
之前,
$fileContents
看起来像什么?它是有效的XML吗?我想加载xmlparser.php。