【问题标题】:YouTube video list in JSON formatJSON 格式的 YouTube 视频列表
【发布时间】:2018-10-10 09:02:48
【问题描述】:

我在 PHP 中创建了一个函数,用于以 JSON 格式获取我的频道的 YouTube 视频列表,但我得到的是空白数组。

public function channels_list()
{

    $method = $_SERVER['REQUEST_METHOD'];
    if($method != 'GET')
    {
        json_output(400,array('status' => 400,'message' => 'Bad request.'));
    }
    else
    {
        $data = array();

        $json_link="https://www.googleapis.com/youtube/v3/search?key='MY API KEY'&channelId='MY CHANNEL ID'&part=snippet,id&order=date&maxResults=10";

        $json = file_get_contents($this->json_link);
        $obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);                        

        foreach($obj['items'] as $post){

            $jsondata['id'] = isset($post['id']['videoId']) ? $post['id']['videoId'] : "";
            $jsondata['published_at'] = isset($post['snippet']['publishedAt']) ? $post['snippet']['publishedAt'] : "";
            $jsondata['title'] = isset($post['snippet']['title']) ? $post['snippet']['title'] : "";
            $jsondata['description'] = isset($post['snippet']['description']) ? $post['snippet']['description'] : "";
            $jsondata['thumbnail'] = "https://i.ytimg.com/vi/{$id}/maxresdefault.jpg";
            array_push($data, $jsondata);

        }
        json_output("200", array("list"=>$data),1);     
    } 
}

【问题讨论】:

    标签: php json youtube


    【解决方案1】:

    试试下面的代码,我正在使用它,它对我来说很好。

    function get_youtube($url){
    
    $youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json";
    
    $curl = curl_init($youtube);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);
    return json_decode($return, true);
    }
    $url = // youtube video url 
    
    // Display Data 
    print_r(get_youtube($url));
    

    更新:

    您不需要 OAuth2 登录。您只需设置 API 密钥即可。

    这是一个 playlistItems->列表请求。

    这里是 api explorer 中的演示:https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.list?part=snippet&playlistId=PLjFEz-E0UPUxw3lFpnfV1dDA7OE7YIFRj&_h=2&

    而不是设置clientId和client Secret

    在公共 API 访问中从云控制台将其 API 密钥设置为您的 API 密钥。

    $client->setAPIKey($API_KEY);
    

    示例:

     $API_key    = 'Your_API_Key';
     $channelID  = 'YouTube_Channel_ID';
     $maxResults = 10;
    
     $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
    

    【讨论】:

    • 嗨 Beulah,我更改了整个代码仍然给我空白数组.. 我选择了您上面提到的获取数据的方式
    • $videoList = json_decode(file_get_contents('googleapis.com/youtube/v3/…));
    • foreach($videoList->items as $item){ $jsondata['data'][$key]['id'] = $item->id->videoId; $jsondata['data'][$key]['published_at'] = $item->sn-p->publishedAt; $jsondata['data'][$key]['title'] = $item->sn-p->title; $jsondata['data'][$key]['description'] = $item->sn-p->description; $jsondata['data'][$key]['thumbnail'] = $item->sn-p->thumbnails->default->url; array_push($data, $jsondata); } } json_output(200, array("list"=>$data),1);
    猜你喜欢
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2018-06-11
    • 2014-01-22
    • 2013-03-21
    相关资源
    最近更新 更多