【问题标题】:php script cant read xml data with colon (:)php 脚本无法读取带有冒号 (:) 的 xml 数据
【发布时间】:2011-08-13 15:39:22
【问题描述】:

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

$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

【问题讨论】:

    标签: php xml namespaces xml-namespaces colon


    【解决方案1】:

    您需要先获取命名空间,然后再尝试访问它的值。看看方法getNamespaces。使用children,您可以访问命名空间内的值:

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

    【讨论】:

    • 我正在关注de2.php.net/manual/en/simplexmlelement.getnamespaces.php 中给出的第一个示例。如果我使用与该示例中相同的xml,它会起作用。但不适用于我的 xml。我想在&lt;?xml version="1.0"?&gt; &lt;q:response xmlns:q="some link"&gt; &lt;q:impression&gt; &lt;q:content&gt; &lt;html&gt; &lt;/html&gt; &lt;/q:content&gt; &lt;q:cpc&gt;0.02&lt;/q:cpc&gt; &lt;/q:impression&gt; &lt;q:app_stats&gt; &lt;q:total&gt; &lt;q:ctr&gt;0&lt;/q:ctr&gt; &lt;q:ecpm&gt;0.0&lt;/q:ecpm&gt; &lt;/q:total&gt; &lt;q:today&gt; &lt;q:ctr&gt;0&lt;/q:ctr&gt; &lt;q:ecpm&gt;0.0&lt;/q:ecpm&gt; &lt;/q:today&gt; &lt;/q:app_stats&gt; &lt;/q:response&gt; 中解析&lt;q:content&gt;&lt;/q:content&gt; 标签内的内容有任何指针吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多