Ich möchte den Titel eines YouTube-Videos in einer Variablen, aber alles, was ich versucht habe, hat nicht funktioniert. Ein Teil des Codes unten gibt den Titel Schnipsel in Variable $ output:Holen Sie Youtube Titel mit PHP-Skript
{ "Artikel": [{ "Schnipsel": { "title": "Hardwell Live at Ultra Music Festival Miami 2016"}} ]}
Aber wie konnte ich nur den Titel in einer Variablen bekommen?
<?php
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$response = curl_download('https://www.googleapis.com/youtube/v3/videos?id=m1ssAFzaCsU&key=AIzaSyBj5GoJlQ4XzebaG6H2tp_WVuQ03JEOOss&fields=items(snippet(title))&part=snippet');
if ($response) {
$xml = new SimpleXMLElement($response);
$title = (string) $xml->title;
echo $title;
} else {
// Error handling.
echo 'error';
}
?>
Was echo $ title 'jetzt zurückkehrt? – Ionut
Es ist kein XML, sondern JSON. – GuyT
Es gibt nichts zurück. – Dave