4

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?

asked Mar 20, 2016 at 9:42
4
  • What you are passing to q? Commented Mar 23, 2016 at 10:36
  • @itzmukeshy7 keyword. Commented Mar 23, 2016 at 11:25
  • exact value of q? Can you show the whole code (with removed API key)? Commented Mar 23, 2016 at 17:54
  • "hardwell%20singles" is the exact value. Commented Mar 23, 2016 at 18:09

1 Answer 1

2
+25

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

answered Mar 24, 2016 at 0:19
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.