youtube data api v3 php search pagination?

前端 未结 2 538
不知归路
不知归路 2020-12-09 22:23

i am trying with youtube api v3 php search...

first time i\'m using this api for this i am beginner...

i have 3 question;

1) how can below search li

2条回答
  •  甜味超标
    2020-12-09 22:56

    The youTube API V3 is somehow complicated compare to API V2.

    To the question above, my approach is not for search result rather is to retrieve user uploaded videos. I believe this can be useful

    References

    The way you create pagination in v3 is not the same as in v2 where you can make your call simply like

    $youtube = "http://gdata.youtube.com/feeds/api/users/Qtube247/uploads?v=2&alt=jsonc&start-index=1&max-results=50";
    

    In v3 you need to make two or three calls the first one will be to get the channel detail and second call will be to retrieve playlist from where we will get the channel playlist Id and finally retrieve individual video data.

    I am using Php CURL

    $youtube = “https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=yourChannelIdgoeshere&key=yourApiKey”;
    

    Here we retrieve user playlist ID

    $result = json_decode($return, true);
    $playlistId=$result['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
    

    we define pagetoken

    $pageToken=’’;
    

    Each time user click control button we retrieve pagetoken from session[] and feed the curl url, and in turn will produce nextpagetoken or prevpagetoken. Whatever you feed the url the Api know what set of list to populate.

    if(isset($_REQUEST['ptk']) && $_REQUEST['ptk’]!==''){
    $pageToken=$_REQUEST['ptk’];
    
    }
    

    Here we retrieve user playlist

    $ playlistItems =”https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&pageToken=”.$pageToken.”&maxResults=50&playlistId=$playlistId&key= yourApiKey”; 
    

    If user has more than maxResult, we should have nextPageToken, take for an example user has 200 uploaded videos,the first pagetoken may be CDIQAA and next pagetoken may be CGQQAA while previous may be CDIQAQ , something like that so is not a number.

    Here we save the pagetoken

    if(isset($result['nextPageToken'])) { $_SESSION[nextToken]=$result['nextPageToken'];
    }
    
    if(isset($result['prevPageToken'])) { $_SESSION[prevToken]=$result['prevPageToken'];
    }
    

    we can then create our control button <>

    $next=$_SESSION[nextToken];
    
    $prev=$_SESSION[prevToken];
    

    The control button here

    ” ><
    
    ” >next>>
    

    From here when user click link it set either next or prev page in session variable (go to up to see how this work)

    To get video duration we use same Php curl

    $videoDetails="https://www.googleapis.com/youtube/v3/videos?part=id,snippet,contentDetails,statistics,status&id=videoIdHere&key=yourApiKey";
    

    $videoData = json_decode($return, true);

    $duration = $videoData['items'][0]['contentDetails']['duration'];

    $viewCount = $videoData['items'][0]['statistics']['viewCount'];

    you may get something like this ('PT2H34M25S')

    I have answer a question Here which show you how to convert the duration data

    See Working Demo Here

提交回复
热议问题