Wednesday, March 30, 2016

Youtube data api v3 not working as expected

Leave a Comment

I am using search api for youtube :

https://developers.google.com/youtube/v3/docs/search/list

I get total of 40191 records and per page I have set max 50.

But I get 17 results every time.

These are the options I am using for getting records:

// SET OPTIONS FOR API $optionsArray = array(         'q' => $_GET['q'],         'maxResults' => 50,         'order' => 'date',         'type' => 'video'     );  // Send request $searchResponse = $youtube->search->listSearch('id, snippet', $optionsArray); 

Also setting pageToken doesnt work. Records are same even after applying nextPageToken. Am I missing something here?

1 Answers

Answers 1

The reason why you get only 17 results is the order property of your request. If you remove this property you get many more results. The order='date' is not working as expected.

A little workaround (only example):

$DEVELOPER_KEY = 'THEKEY'; $client = new Google_Client(); $client->setDeveloperKey($DEVELOPER_KEY);  //Define an object that will be used to make all API requests. $youtube = new Google_Service_YouTube($client);  try {     // Call the search.list method to retrieve results matching the specified     // query term.     $searchResponse = $youtube->search->listSearch('id,snippet', array(         'q' => $_GET['q'],         'maxResults' => $_GET['maxResults'],         'type' => 'video'     ));      function sortResponse($date1, $date2) {         $date1 = new DateTime($date1['snippet']['publishedAt']);         $date2 = new DateTime($date2['snippet']['publishedAt']);         //"<" - sort DESC         //">" - sort ASC         return $date1 < $date2;     }      $items = $searchResponse['items'];     usort($items, "sortResponse");      //now your sorted items are in $items... } 

You can merge all your repsonse items in one array and use the custom sort function to get the order by publishAt.

API Document:
https://developers.google.com/youtube/v3/docs/search/list?hl=de#parameters

Related Question:
When I use order-date in youtube api then there are total results but items are not found

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment