/** * Perform a search on youtube. Passes the result feed to echoVideoList. * * @param string $searchType The type of search to perform. * If set to 'owner' then attempt to authenticate. * @param string $searchTerm The term to search on. * @param string $startIndex Start retrieving search results from this index. * @param string $maxResults The number of results to retrieve. * @return void */ function searchVideos($searchType, $searchTerm, $startIndex, $maxResults) { // create an unauthenticated service object $youTubeService = new Zend_Gdata_YouTube(); $query = $youTubeService->newVideoQuery(); $query->setQuery($searchTerm); $query->setStartIndex($startIndex); $query->setMaxResults($maxResults); switch ($searchType) { case 'most_viewed': $query->setFeedType('most viewed'); $query->setTime('this_week'); $feed = $youTubeService->getVideoFeed($query); break; case 'most_recent': $query->setFeedType('most recent'); $query->setTime('this_week'); $feed = $youTubeService->getVideoFeed($query); break; case 'recently_featured': $query->setFeedType('recently featured'); $feed = $youTubeService->getVideoFeed($query); break; case 'top_rated': $query->setFeedType('top rated'); $query->setTime('this_week'); $feed = $youTubeService->getVideoFeed($query); break; case 'username': $feed = $youTubeService->getUserUploads($searchTerm); break; case 'all': $feed = $youTubeService->getVideoFeed($query); break; case 'owner': $httpClient = getAuthSubHttpClient(); $youTubeService = new Zend_Gdata_YouTube($httpClient); try { $feed = $youTubeService->getUserUploads('default'); if (loggingEnabled()) { logMessage($httpClient->getLastRequest(), 'request'); logMessage($httpClient->getLastResponse()->getBody(), 'response'); } } catch (Zend_Gdata_App_HttpException $httpException) { print 'ERROR ' . $httpException->getMessage() . ' HTTP details<br /><textarea cols="100" rows="20">' . $httpException->getRawResponseBody() . '</textarea><br />' . '<a href="session_details.php">' . 'click here to view details of last request</a><br />'; return; } catch (Zend_Gdata_App_Exception $e) { print 'ERROR - Could not retrieve users video feed: ' . $e->getMessage() . '<br />'; return; } echoVideoList($feed, true); return; default: echo 'ERROR - Unknown search type - \'' . $searchType . '\''; return; } if (loggingEnabled()) { $httpClient = $youTubeService->getHttpClient(); logMessage($httpClient->getLastRequest(), 'request'); logMessage($httpClient->getLastResponse()->getBody(), 'response'); } echoVideoList($feed); }
case 'most_viewed': $query->setFeedType('most viewed'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'most_recent': $query->setFeedType('most recent'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'recently_featured': $query->setFeedType('recently featured'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'top_rated': $query->setFeedType('top rated'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'all': $feed = $yt->getVideoFeed($query); break; default: echo 'ERROR - unknown queryType - "' . $queryType . '"'; break; } echoVideoList($feed); } ?>
function searchYoutube($cat, $searchTerm, $page, $queryType, $maxResults, $startIndex) { $retval = array(); $data = array(); if ($queryType === null) { /* display the entire interface */ include '../home.html'; } else { if ($queryType == 'show_video') { /* display an individual video */ if (array_key_exists('videoId', $_REQUEST)) { $videoId = $_REQUEST['videoId']; $data = echoVideoPlayer($videoId); } else { echo 'No videoId found.'; exit; } } else { /* display a list of videos */ //$searchTerm = $_REQUEST['searchTerm']; //$startIndex = $_REQUEST['startIndex']; //$maxResults = $_REQUEST['maxResults']; $yt = new Zend_Gdata_YouTube(); $query = $yt->newVideoQuery(); $query->setQuery($searchTerm); $query->setStartIndex($startIndex); $query->setMaxResults($maxResults); //print_r($query); /* check for one of the standard feeds, or list from 'all' videos */ switch ($queryType) { case 'most_viewed': $query->setFeedType('most viewed'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'most_recent': $query->setFeedType('most recent'); $feed = $yt->getVideoFeed($query); break; case 'recently_featured': $query->setFeedType('recently featured'); $feed = $yt->getVideoFeed($query); break; case 'top_rated': $query->setFeedType('top rated'); $query->setTime('this_week'); $feed = $yt->getVideoFeed($query); break; case 'all': $feed = $yt->getVideoFeed($query); break; default: echo 'ERROR - unknown queryType - "' . $queryType . '"'; break; } $data = echoVideoList($feed); //youtube won't allow search item beyond 1000, namely no page beyond 100 if 10 items/page. $totalpages = min(100, $feed->getTotalResults()->text); } } $retval['status'] = 'OK'; $retval['statusmsg'] = 'OK'; $retval['data'] = $data; $retval['totalpages'] = $totalpages; return $retval; }