Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Parsing - Fatal编程技术网

Php 正确解析XML

Php 正确解析XML,php,xml,parsing,Php,Xml,Parsing,我正在使用Songkick API,需要解析URL中的XML数据,以获取嵌套URL并重定向到该URL。我似乎无法正确地解析XML。这是我现在拥有的 1. <?php 2. $songkick_artist = $_GET['id']; 3. 4. $xmlDoc = new DOMDocument(); 5. $xmlDoc->load("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_

我正在使用Songkick API,需要解析URL中的XML数据,以获取嵌套URL并重定向到该URL。我似乎无法正确地解析XML。这是我现在拥有的

1.  <?php
2.  $songkick_artist = $_GET['id'];
3. 
4.  $xmlDoc = new DOMDocument(); 
5.  $xmlDoc->load("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_artist&apikey=songkickapikey"); 
6.
7.  $x = $xmlDoc->documentElement; 
8.  foreach ($x->childNodes AS $item) 
9.  {  
10. print $item->[]  
11. print $item->nodeName . " = " . $item->nodeValue . "<br />"; 
12. }
13. ?>
1。负载(“http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_艺人&apikey=songkickapikey”);
6.
7.  $x=$xmlDoc->documentElement;
8.foreach($x->childNodes作为$item)
9{  
10.打印$item->[]
11.打印$item->nodeName。“=”$item->nodeValue。“
”; 12. } 13. ?>
我得到这个错误:

分析错误:语法错误,意外的“[”,应为T_字符串 或T_变量或第10行的“{”或“$”

解析XML信息的方法太多了,我现在很困惑。在此方面的任何帮助都将不胜感激

更新日期4-13-11

我现在将代码更改为:

<?php   
$doc = new DomDocument;

// We must validate the document before referring to the id
$doc->validateOnParse = true;
$doc->Load("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_artist&apikey=songkickapikey");

echo "The element whose id is myelement is: " . 
$doc->getElementById('myelement')->tagName . "\n";
?>
getElementById('myelement')->标记名。“\n”;
?>
当$doc->Load(“URL”);被激发时,我得到一个403禁止的错误


给出了什么?

您的问题是一个简单的解析错误。
第10行中缺少;这可能会解决问题?

第10行中您想做什么?您的syntag错误:
打印$item->[]

$item是一个对象,因此可以使用$item->doSomeMethod()执行doSomeMethod,但不能写入$item->[]。

解决了这个问题

$request_url=urlencode(“http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_艺人&apikey=songkickapikey”); $xml=simplexml_load_file($request_url)或die(“提要未加载”); $artist=$xml->results->artist[“uri”]


urlencode是此项的主键,因为url无法正确解析…

请检查语法。错误消息表明第10行出现语法错误:
print$item->[]
。在这种情况下,这与解析XML无关。我不确定API在提出此问题时是否缺少JSON支持,但现在支持JSON,而且JSON比XML更受欢迎。嗨,Wesley,我已经尝试过了,这不是问题。看起来MRu在这里,但我能做些什么来获得我需要的结果?