Я хотел бы получить название видео с youtube в переменной, но все, что я пробовал, не работает. Часть кода ниже возвращает название фрагмента в переменной $ выхода:Получите название youtube с помощью php-скрипта
{ "элементы": [{ "фрагмент": { "Название": "Hardwell Живите на Ultra Music Festival в Майами 2016 года"}} ]}
Но как я могу получить только заголовок в переменной?
<?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';
}
?>
Что эхо $ названия "возвращает в настоящее время? – Ionut
Это не XML, а JSON. – GuyT
Он ничего не возвращает. – Dave