我正在使用simpleXML来解析这个xml文件.这是我用来访问YouTube API的Feed.我想将最新的视频嵌入到对象中并显示接下来的四个缩略图.
所以我正在使用simplexml_load_file它,使用foreach循环来访问每个feed中的值.
我可以访问这些值没有问题,但我遇到的问题是它将每个视频存储在一个单独的SimpleXMLElement Object.我无法控制我正在访问哪个对象,因为它们没有存储在数组中.所以我不能说,$thumb[4]或者说$entry[4]->thumb.
我尝试使用SimpleXMLIterator,但无论出于何种原因,具有相同开头的任何值都呈现为空白.例如,视频可能有以下十一种变体:
而这些将分别呈现为[1]="",[2]="",[3]="",等.
我很乐意为任何可以提供帮助的人提供更多信息!
编辑
这是我的结果的print_r.这是在一个变量上完成的,让您了解我面临的结构问题.整个print_r($ entry)将为XML文件中的每个节点提供变量.
SimpleXMLElement Object
(
[0] => 159
)
SimpleXMLElement Object
(
[0] => 44
)
Run Code Online (Sandbox Code Playgroud)
此外,print_r只是在PHP块内部进行测试.我实际上是想在HTML中的回声中访问变量.
我需要货币兑换,欧元兑换美元.
欧洲中央银行提供的费率如下:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我可以通过使用第一个节点获得美元汇率,但如果他们更改订单怎么办?
我需要更可靠的东西吗?我不知道怎么做..
$xml = @simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
echo "dollar: " . $xml->Cube->Cube->Cube[0]->attributes()->rate;
Run Code Online (Sandbox Code Playgroud) 我基本上需要加载整个XML文件,添加一个包含内容的新行,然后保存它.但我想知道哪一个更快.. XMLWriter还是SimpleXML?哦,而且,它主要是大型XML文件,超过10MB.
谢谢!
<contact:addr>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:city></contact:city>
<contact:pc></contact:pc>
<contact:cc></contact:cc>
</contact:addr>
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我们可以看到我们确实有三倍的元素街; 有没有办法,通过使用simpleXML,正确访问,例如,第二个街道元素?
在此先感谢,MEM
我正在尝试使用simplexml从服务器加载xml文件:
$xml = simplexml_load_file($xml_query);
Run Code Online (Sandbox Code Playgroud)
服务器需要HTTP身份验证,所以(不出所料)我收到一条错误消息:
HTTP请求失败!HTTP/1.1 401未经授权
是否可以使用此功能将我的用户名和密码传递给服务器?
编辑
如果有人能告诉我一个更安全的方法来做到这一点,我将不胜感激.
我有一个包含数据的XML文件并运行以下代码:
<?php
$xml = simplexml_load_file("index.xml");
$result = $xml->xpath("product");
echo $result[0]->name . '<br />';
echo $result[0]->price . '<br />';
echo $result[0]->info-url . '<br />';
echo $result[0]->image-url . '<br />';
echo $result[0]->longdescription . '<br />';
?>
Run Code Online (Sandbox Code Playgroud)
我的XML文件的一部分
<product>
<name>Test</name>
<price>100</price>
<info-url>http://www.test.com</info-url>
<image-url>http://www.test.com/image.jpg</image-url>
</product>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
" 注意:使用未定义的常量url - 在第11行的C:\ wamp\www\concepts\xml-to-array\index.php中假定为'url' "
它不喜欢减号.我怎么得到它?
我使用curl将XML文件发布到服务器.Everthing工作完美,但我不知道如何处理响应的最佳方式.我想用simpleXML解析它.有了这个要求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost);
curl_setopt($ch, CURLOPT_POST, 1);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
print_r($data);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我得到这个作为响应字符串(浏览器源视图)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Expires: Sat, 6 May 1995 12:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: JSESSIONID=5FD40C8D8F5EDC85E57AF39E877BE564; Path=/upp/; Secure; HttpOnly
Content-Type: text/xml;charset=UTF-8
Content-Length: 829
Date: Sun, 22 Apr 2012 08:59:10 GMT
<?xml version='1.0' encoding='UTF-8'?>
<authorizationService …Run Code Online (Sandbox Code Playgroud) 我有一个XML文档,其中的标签带有两个破折号,如下所示:<item--1>。我正在使用SimpleXML来解析此文档,因此它为我提供了带有标签名称的对象属性。我想这显然是个问题,因为破折号对于变量和属性名称是无效字符。
<?php
$xml = "<data><fruits><item>apple</item><item--1>bananna</item--1></fruits></data>";
$xml = simplexml_load_string($xml);
foreach( $xml->children() as $child ) {
var_dump($child->item);
# var_dump($child->item--1);
}
Run Code Online (Sandbox Code Playgroud)
运行此命令时,
object(SimpleXMLElement)#5 (1) {
[0]=>
string(5) "apple"
}
Run Code Online (Sandbox Code Playgroud)
但是,如果取消注释最后一行,即带有两个破折号的xml元素,则会出现错误:
PHP Parse error: syntax error, unexpected T_LNUMBER in test.php on line 17
Run Code Online (Sandbox Code Playgroud)
我尝试使用花括号:
var_dump($child->{item--1});
Run Code Online (Sandbox Code Playgroud)
但这只给了我这个错误:
PHP Parse error: syntax error, unexpected T_DEC
Run Code Online (Sandbox Code Playgroud)
是减量运算符,或--。
如何引用该对象的属性?
我试图从youtube使用api获取discriptiobn ....但我不知道我错在哪里它没有获得单个视频.但如果我尝试播放列表它的工作正常,而不是视频ID
这是我的代码
error_reporting(E_ALL);
$feedURL = 'https://gdata.youtube.com/feeds/api/playlists/'.$id.'?v=2&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码正在使用播放列表...但如果我尝试一个视频它不工作:
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$id.'?v=2&alt=json&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从[此URL(加载相当长的时间)] [1]中获取以下节点的值。我感兴趣的元素是:
title, g:price and g:gtin
Run Code Online (Sandbox Code Playgroud)
XML开始如下:
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>PhotoSpecialist.de</title>
<link>http://www.photospecialist.de</link>
<description/>
<item>
<g:id>BEN107C</g:id>
<title>Benbo Trekker Mk3 + Kugelkopf + Tasche</title>
<description>
Benbo Trekker Mk3 + Kugelkopf + Tasche Das Benbo Trekker Mk3 ist eine leichte Variante des beliebten Benbo 1. Sein geringes Gewicht macht das Trekker Mk3 zum idealen Stativ, wenn Sie viel draußen fotografieren und viel unterwegs sind. Sollten Sie in eine Situation kommen, in der maximale Stabilität zählt, verfügt das Benbo Trekker Mk3 über einen …Run Code Online (Sandbox Code Playgroud) php ×10
simplexml ×10
xml ×4
youtube-api ×2
curl ×1
http ×1
nodes ×1
performance ×1
post ×1
properties ×1
syntax ×1
xmlreader ×1
xmlwriter ×1