Search code examples
phplaravellaravel-4youtube-api

Get youtube channels by name


I am facing a problem for last few hours but not come to the solution. I am trying to get youtube channel by name using php-youtube-api reference link in laravel. But I am only getting single channel in output. But i want a list of channels related name like this. When i search for any channel having name "soccerit" return me

stdClass Object
(
[kind] => youtube#channel
[etag] => "kjEFmP90GvrCl8BObMQtGoRfgaQ/I7mfLJg2NMc3uuc6rLgQDgQ-_3g"
[id] => UClENohulIkpi8JV18UJIPeg
[snippet] => stdClass Object
    (
        [title] => soccer
        [description] => 
        [publishedAt] => 2005-11-07T19:54:17.000Z
        [thumbnails] => stdClass Object
            (
                [default] => stdClass Object
                    (
                        [url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s88-c-k-no/photo.jpg
                    )

                [medium] => stdClass Object
                    (
                        [url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s240-c-k-no/photo.jpg
                    )

                [high] => stdClass Object
                    (
                        [url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s240-c-k-no/photo.jpg
                    )

            )

    )

[contentDetails] => stdClass Object
    (
        [relatedPlaylists] => stdClass Object
            (
                [uploads] => UUlENohulIkpi8JV18UJIPeg
            )

        [googlePlusUserId] => 107844494883714037142
    )

[statistics] => stdClass Object
    (
        [viewCount] => 763486
        [commentCount] => 134
        [subscriberCount] => 1478
        [hiddenSubscriberCount] => 
        [videoCount] => 32
    )

)

Thanks in advance.

my code is :

YoutubeController.php

class YoutubeController extends BaseController {

    public function index()
    {
        $youtube = new Madcoda\Youtube(array('key' => ##Yuxw99Ka7szK4'));

        $channel = $youtube->getChannelByName($data['q']);
        print_r($channel);
    }
 }

Youtube.php

 public function getChannelByName($username, $optionalParams = false)
 {


    $API_URL = $this->getApi('channels.list');

    $params = array(
        'forUsername' => $username,
        'part' => 'id,snippet,contentDetails,statistics,invideoPromotion'
    );


    if($optionalParams){
        $params = array_merge($params, $optionalParams);
    }


    $apiData = $this->api_get($API_URL, $params);

    return $this->decodeSingle($apiData);
}

public function api_get($url, $params)
{
    //set the youtube key
    $params['key'] = $this->youtube_key;

    //boilerplates for CURL
   // $url = "https://gdata.youtube.com/feeds/api/channels";
    $a = $url . (strpos($url, '?') === false ? '?' : '') . http_build_query($params);
    $tuCurl = curl_init();
    curl_setopt($tuCurl, CURLOPT_URL, $a);

    if (strpos($url, 'https') === false) {
        curl_setopt($tuCurl, CURLOPT_PORT, 80);
    } else {
        curl_setopt($tuCurl, CURLOPT_PORT, 443);
    }
    curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 0);

    $tuData = curl_exec($tuCurl);
    if (curl_errno($tuCurl)) {
        throw new \Exception('Curl Error : ' . curl_error($tuCurl));
    }
    return $tuData;
}

Solution

  • It seems you are using wrong method. Method getChannelByName returns channel with exact name. If you want to search channels you should use:

    // Search playlists, channels and videos, Return an array of PHP objects
    $channels = $youtube->search($data['q']);
    

    or if you want co search only videos you should choose:

    // Search only Videos, Return an array of PHP objects
    $channels = $youtube->searchVideos($data['q']);