0

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>';    }
    }
}

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

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I am following the 1st example given in de2.php.net/manual/en/simplexmlelement.getnamespaces.php . It works if I use the same xml as given in that example. But does not work for my xml. I want to parse the content inside <q:content></q:content> tags in <?xml version="1.0"?> <q:response xmlns:q="some link"> <q:impression> <q:content> <html> </html> </q:content> <q:cpc>0.02</q:cpc> </q:impression> <q:app_stats> <q:total> <q:ctr>0</q:ctr> <q:ecpm>0.0</q:ecpm> </q:total> <q:today> <q:ctr>0</q:ctr> <q:ecpm>0.0</q:ecpm> </q:today> </q:app_stats> </q:response> Any pointers? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.