php - 使用PHP脚本获取YouTube标题

标签 php youtube youtube-data-api

我想用一个变量来获取youtube视频的标题,但我尝试的所有方法均无效。以下代码的一部分返回变量$ output中的标题片段:

{ "items": [ { "snippet": { "title": "Hardwell Live at Ultra Music Festival Miami 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';
}

?>

最佳答案

$output是一个JSON字符串,请使用json_decode对其进行解析:

$output = '{ "items": [ { "snippet": { "title": "Hardwell Live at Ultra Music Festival Miami 2016" } } ] }';
$output_decoded = json_decode($output);
$title = $output_decoded->items[0]->snippet->title;
// $title is now 'Hardwell Live at Ultra Music Festival Miami 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) {

    $response_decoded = json_decode($response);
    $title = $response_decoded->items[0]->snippet->title;
    echo $title;

} else {

    // Error handling.

                echo 'error';
}

?>

关于php - 使用PHP脚本获取YouTube标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38654185/

相关文章:

php - 有效搜索 php 迭代器

youtube - 列出 channel 中的订阅者

api - 如何使用YouTube API获取每个YouTube channel 的 channel 类型或类别?

python - YouTube 数据 API : breaking a nextPageToken while loop if quota limit is reached?

php - youtube api maxResults

php - 获取数据库中刚刚插入的行的id

php - 需要帮助规划任务管理网站

php - 黑钻问号 - Youtube API - J�r�my

html - 没有黑条的响应式全屏 youtube 视频?

php - 如何使直接 youtube 视频 url 工作不嵌入?