App\services\YouTube::search PHP Method

    public function search($q, $pageToken = '', $perPage = 10)
    {
        if (!$this->enabled()) {
            return false;
        }
        $uri = sprintf('search?part=snippet&type=video&maxResults=%s&pageToken=%s&q=%s', $perPage, urlencode($pageToken), urlencode($q));
        $cacheKey = md5("youtube_{$uri}");
        if ($response = Cache::get($cacheKey)) {
            return $response;
        }
        if ($response = $this->get($uri)) {
            // Cache the result for 7 days
            Cache::put($cacheKey, $response, 60 * 24 * 7);
        }
        return $response;
    }

Usage Example

Exemplo n.º 1
0
 public function testSearch()
 {
     $this->withoutEvents();
     $client = m::mock(Client::class, ['get' => new Response(200, [], file_get_contents(dirname(__FILE__) . '/blobs/youtube/search.json'))]);
     $api = new YouTube(null, $client);
     $response = $api->search('Lorem Ipsum');
     $this->assertEquals('Slipknot - Snuff [OFFICIAL VIDEO]', $response->items[0]->snippet->title);
     // Is it cached?
     $this->assertNotNull(Cache::get('1492972ec5c8e6b3a9323ba719655ddb'));
 }