Неустранимая ошибка: неперехваченная ошибка: вызов атрибутов функции-члена () с нулевым значением при извлечении видео YouTube из канала XML - PullRequest
0 голосов
/ 16 июня 2020

Я создаю веб-сайт с автоматически обновляемым встраиванием YouTube (когда на канал загружается новое видео, встраивание автоматически обновляется).

Мне удалось его получить работаю в целом, вытягивая подачу, эт c. с использованием cURL и SimpleXmlElement.

При вводе последнего бита кода для разбиения URL-адреса на переменную возникает указанная выше ошибка.

Используемый мной код функции - это следующее:

function curl_get_contents($url) {
    // Initiate the curl session
    $ch = curl_init();
    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //set timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    // Execute the curl session
    $output = curl_exec($ch);
    // Close the curl session
    curl_close($ch);
    // Return the output as a variable
    return $output;
}

$feed = curl_get_contents("https://www.youtube.com/feeds/videos.xml?user=CHANNEL_NAME");
$xml = new SimpleXmlElement($feed);
$url = $xml->entry[$i]->link->attributes();
$videourl = explode("&",$url['href']);
$video = str_replace("https://www.youtube.com/watch?v=","",$videourl[0]);

Я также использую следующий код для отображения вставки на странице:

<iframe class="video" src="https://www.youtube.com/embed/'.$video.'" frameborder="0" allowfullscreen></iframe>

Вероятно, это очень простая вещь, которую я не смог сделать, но после многих часов работы над кодом я не могу понять, где я ошибся.

Любая помощь будет очень признательна.

1 Ответ

0 голосов
/ 16 июня 2020

Рабочий код для вашего. attributes() возвращает array (), к которому вы можете получить доступ href, как показано ниже.

PHP

function curl_get_contents($url) {
    // Initiate the curl session
    $ch = curl_init();
    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //set timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    // Execute the curl session
    $output = curl_exec($ch);
    // Close the curl session
    curl_close($ch);
    // Return the output as a variable
    return $output;
}

$feed = curl_get_contents("https://www.youtube.com/feeds/videos.xml?user=CHANNEL_NAME");

$xml = new SimpleXmlElement($feed);
$url = $xml->entry[0]->link->attributes()['href'];
var_dump($url);

HTML

<iframe class="video" src="'.$url.'" frameborder="0" allowfullscreen></iframe> 

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...