Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Youtube API mittels PHP Videos suchen

Empfohlene Antworten

Veröffentlicht

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]

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.