youtube data api v3 php search pagination?

前端 未结 2 501
不知归路
不知归路 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:55

    1) how can below search list showing pagination numbers? (per page 50 result)

    You need to write your own cacheing logic to implement this feature because with every result you get two tokens "NextPageToken" and "PreviousPageToken" and subsequent query must contain that token number to get next page or previous page token like below.

    So whenever results are not available at cache then you should send either nextpagetoken or previous page token.

    https://www.googleapis.com/youtube/v3/search?key=API_KEY&part=snippet&q=japan&maxResults=10&order=date&pageToken=NEXT_or_PREVIOUS_PAGE_TOKEN

    In particular your case where you need 50 pages per page and you are showing 3 pagination like (1,2,NEXT) then you need to fetch results two times. Both the results you will keep in cache so for page 1 and 2 results will be retrieved from cache. For next you make it sure that you are making query google again by sending nextPageToken.

    Thus to show pagination 1-n and every page 50 results then you need to make n-1 queries to google api. But if you are showing 10 results per page then you cane make single query of 50 results using which you can show first 5 pages (1-5) with the help of retrieved results and at next you should again send next page token like above.

    NOTE- Google youtube api provide 50 results max.

    2) how can video duration show in list? (3:20 min:second)

    Youtube API v3 do not return video duration at simple first search response. To get video duration we need to make one extra call to youtube api like below.

    https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID1%2CVIDEO_ID2&part=contentDetails&key=API_KEY (max 50 IDs)

    This issue is highlighted in "http://code.google.com/p/gdata-issues/issues/detail?id=4294".I posted my answer here too.

    Hence if we want to display video duration then we need to make two calls every time.

    3) how can order viewCount

    Trigger below query it will provide results ordered by view count.

    https://www.googleapis.com/youtube/v3/search?key=KEY&part=snippet&q=japan&maxResults=5&order=viewCount

    For detail please refer this - https://developers.google.com/youtube/v3/docs/search/list#order

    0 讨论(0)
  • 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

    <a href=”?ptk=<?php echo $prev?>” ><<prev</a>
    
    <a href=”?ptk=<?php echo $next?>” >next>></a>
    

    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

    0 讨论(0)
提交回复
热议问题