PHP脚本无法使用冒号(:)读取xml数据 [英] php script cant read xml data with colon (:)

查看:83
本文介绍了PHP脚本无法使用冒号(:)读取xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试读取youtube xml数据宽​​度的php,但由于youtube在其API中使用冒号(:)而卡住了 确切地说,这是我使用的脚本:

I tried to read youtube xml data width php but stuck because youtube uses colon (:)in his API To be precise, here is script I use:

$video = array('/some arrays here,separated width commas/');// array of youtube videos

$v = preg_replace("/[^A-Za-z0-9\-\_]/","",$_GET["v"]); // make sure "v" parameter is sanitized

if(!empty($v) && in_array($v,$video)){ //check if parameter "v" is empty and is yours video
    echo('<object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/'.$v.'&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$v.'&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>');     
}else{
    foreach($video as $v){
        echo('
        <div class="tube"><a href="/pages/tube.php?v='.$v.'" class="thickbox"><img style="border:2px solid #000;margin:5px;" alt="" src="http://i1.ytimg.com/vi/'.$v.'/default.jpg"/></a>');
        $xmlData = simplexml_load_string(file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$v.'?fields=title,yt:recorded'));//it should be working if I add fields just separated width commas

        $title = (string)$xmlData->title;
        $recorded = (string)$xmlData->recorded;//It should be yt:recorded but I understand that cant use : here!

        {        echo '<p>'.$recorded.' '.$title.'</p></div>';    }
    }
}

它的宽度很好,只是标题.是否有可能以某种方式读取yt:recorded

It works fine width just title. Is it possible to read somehow yt:recorded

推荐答案

在尝试访问名称空间之前,需要先获取该名称空间.看看方法 getNamespaces .然后,您可以使用children访问名称空间中的值:

You need to fetch the namespace first before trying to access it's values. Have a look at the method getNamespaces. Using children you can then access values inside a namespace:

$namespaces = $entry->getNameSpaces(true);
$yr = $entry->children($namespaces['yt']); 
echo $yr->recorded;

这篇关于PHP脚本无法使用冒号(:)读取xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆