Veröffentlicht 12. Juli 201312 j Hallo, ich versuche gerade mittels der Youtube API 2 und PHP mehrere Videos, nach Views absteigend geordnet auszugeben. Das klappt bis auf die Views und Likes? Habe schon viel gegoogelt verstehe aber nicht woher ich diese Werte bekomme. Vielen Dank im Voraus. function getyoutubevideos($search,$orderby,$start,$count){ $search = urlencode($search); $orderby = urlencode($orderby); $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?q='.$search.'&orderby='.$orderby.'&start-index='.$start.'&v=2'); foreach($xml->entry as $video){ $res[] = $video; } return $res; } foreach(getyoutubevideos("raspberry pi","viewCount",1,20) as $video){ echo "<pre>"; print_r($video); echo "Title: " . $video->title . "<br>"; echo "Anzahl Views: " . "Unbekannt" . "<br>"; echo "Anzahl Likes: " . "Unbekannt" . "<br>"; echo "Username: " . $video->author->name . "<br>"; echo "Link zum Video: " . $video->link['href'] . "<br>"; echo "</pre>"; }[/PHP]
12. Juli 201312 j Die Daten die du haben willst liegen in einem anderen Namespace des XML Responses. Das bedeutet dann du musst aktiv in diesen Namespace wechseln und dann die die notwendigen Attribute zusammen suchen. Hier eine recht gute Erklärung dazu zwar am Media Namespace aber das ganze lässt dich auch auf den yt Namespace anwenden. http://alisothegeek.com/2011/07/picking-apart-xml-feeds-and-namespaces-with-php-and-simplexml/ function getyoutubevideos($search,$orderby,$start,$count){ $search = urlencode($search); $orderby = urlencode($orderby); $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?q='.$search.'&orderby='.$orderby.'&start-index='.$start.'&v=2'); foreach($xml->entry as $video){ $res[] = $video; } return $res; } foreach(getyoutubevideos("raspberry pi","viewCount",1,20) as $video){ //Namespaces die im xml vorhanden sind sammeln $namespaces = $video->getNameSpaces( true ); //Namespace yt selektieren $youtubeData = $video->children( $namespaces['yt'] ); echo "<pre>"; //print_r($video); echo "Title: " . $video->title . "<br>"; //so kannst du drauf zugreifen echo "Anzahl Views: " . $youtubeData->statistics->attributes()->viewCount . "<br>"; echo "Anzahl Likes: " . $youtubeData->rating->attributes()->numLikes . "<br>"; echo "Username: " . $video->author->name . "<br>"; echo "Link zum Video: " . $video->link['href'] . "<br>"; echo "</pre>"; } [/PHP]
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.