Alaouy\Youtube\Youtube::getRelatedVideos PHP Method

getRelatedVideos() public method

public getRelatedVideos ( string $videoId, $maxResults = 5, $part = ['id', 'snippet'] ) : array
$videoId string
return array
    public function getRelatedVideos($videoId, $maxResults = 5, $part = ['id', 'snippet'])
    {
        if (empty($videoId)) {
            throw new \InvalidArgumentException('A video id must be supplied');
        }
        $API_URL = $this->getApi('search.list');
        $params = array('type' => 'video', 'relatedToVideoId' => $videoId, 'part' => implode(', ', $part), 'maxResults' => $maxResults);
        $apiData = $this->api_get($API_URL, $params);
        return $this->decodeList($apiData);
    }

Usage Example

Example #1
0
 public function testGetRelatedVideos()
 {
     $limit = rand(3, 10);
     $vID = 'rie-hPVJ7Sw';
     $response = $this->youtube->getRelatedVideos($vID, $limit);
     $this->assertEquals($limit, count($response));
     $this->assertNotNull('response');
     $this->assertEquals('youtube#searchResult', $response[0]->kind);
     $this->assertEquals('youtube#video', $response[0]->id->kind);
     //add all these assertions here in case the api is changed,
     //we can detect it instantly
     $this->assertObjectHasAttribute('snippet', $response[0]);
 }