1
votes

I want to pull channel information based on a username or channel id. This information is derived from the URL used to view that channel.

EX: https://www.youtube.com/user/csga5000, or, https://www.youtube.com/channel/some-channel-id

I have this code:

$this->load->library('APIs/YouTube');
echo "[";
echo json_encode($this->youtube->getId('channel/UCkklJA9WbtJM5-g21xHI3yA'),false);
echo "\n]";

The function called is:

public function getId($url) {
    $id = explode('/', $url);
    echo json_encode($id).',';

    //User's channel
    if ($id[0] === 'user') {
        $response = $this->youtube->channels->listChannels('id,snippet',array(
            'forUsername' => $id[1]
        ));
    }
    //Channel id
    else {
        $response = $this->youtube->channels->listChannels('id,snippet',array(
            'id' => $id[1],
            'maxResults' => 2
        ));
    }

    return $response;
}

Here is the construct function for the youtube class:

public function __construct()
{
    $this->client = new Google_Client();
    $this->client->setDeveloperKey($this->api_key);
    $this->client->setClientId($this->client_id);
    $this->client->setClientSecret($this->client_secret);
    $this->client->setScopes(array(
        Google_Service_Oauth2::PLUS_LOGIN,
        Google_Service_Oauth2::PLUS_ME,
        Google_Service_Oauth2::USERINFO_EMAIL,
        Google_Service_Oauth2::USERINFO_PROFILE,
        Google_Service_YouTube::YOUTUBE
    ));

    $ci =& get_instance();
    $this->client->setRedirectUri($ci->config->item('base_url').'auth/youtube/');

    $this->oauth = new Google_Service_Oauth2($this->client);

    $this->youtube = new Google_Service_YouTube($this->client);
}

Calling the function with 'user/csga5000' doesn't work either

The results printed are:

[
    [
        "channel",
        "UCkklJA9WbtJM5-g21xHI3yA"
    ],
    {
        "etag":"\"IHLB7Mi__JPvvG2zLQWAg8l36UU\/KcPrlZVHCJ9bAKurpGOj1BBEH6g\"",
        "eventId":null,
        "kind":"youtube#channelListResponse",
        "nextPageToken":null,
        "prevPageToken":null,
        "visitorId":null
    }
]

I just want results like this:

https://www.googleapis.com/youtube/v3/channels?part=id%2Csnippet&id=UCkklJA9WbtJM5-g21xHI3yA&key={YOUR_API_KEY}

or

https://www.googleapis.com/youtube/v3/channels?part=id%2Csnippet&forUsername=csga5000&key={YOUR_API_KEY}

Which you can test here: https://developers.google.com/youtube/v3/docs/channels/list#try-it

With forUsername csga5000 or id UCkklJA9WbtJM5-g21xHI3yA

All the examples I found used "mine => true" and oauth to load this data, so nothing on google, or stack overflow seemed helpful.

1

1 Answers

1
votes

I was just missing something silly, I'd been super confused by the response.

I needed the line:

$response->getItems();