V
V
Vlad Lopushenko2018-08-30 21:40:35
Google
Vlad Lopushenko, 2018-08-30 21:40:35

Google api (youtube/v3/subscriptions) got the user id how to get the list of channels to which he is subscribed?

Help with google api.
I work with google api for the first time.
I authorize a person on the site, I get his id,
how to get a list of channels to which he is subscribed?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vlad Lopushenko, 2018-08-31
@vald5116

Solved the problem Documentation thank you all!

D
Dimonchik, 2018-08-30
@dimonchik2013

no
private infa

P
pyro338, 2018-11-08
@pyro338

well I did something like this:

public function getSubscriptionsById($channelId, $part = ['contentDetails', 'id', 'snippet', 'subscriberSnippet'])
    {
        $API_URL = 'https://www.googleapis.com/youtube/v3/subscriptions';
        $params = [
            'channelId' => $channelId,
            'part' => implode(', ', $part),
            'maxResults' => 40
        ];

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

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

    /**
     * Using CURL to issue a GET request
     *
     * @param $url
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    public function api_get($url, $params)
    {
        //set the youtube key
        $params['key'] = \Illuminate\Support\Facades\Config::get('youtube.key');

        //boilerplates for CURL
        $tuCurl = curl_init();
        curl_setopt($tuCurl, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : '') . http_build_query($params));
        if (strpos($url, 'https') === false) {
            curl_setopt($tuCurl, CURLOPT_PORT, 80);
        } else {
            curl_setopt($tuCurl, CURLOPT_PORT, 443);
        }

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

        return $tuData;
    }

    /**
     * Decode the response from youtube, extract the multiple resource object.
     *
     * @param  string $apiData the api response from youtube
     * @throws \Exception
     * @return \StdClass  an Youtube resource object
     */
    public function decodeMultiple(&$apiData)
    {
        $resObj = json_decode($apiData);
        if (isset($resObj->error)) {
            $msg = "Error " . $resObj->error->code . " " . $resObj->error->message;
            if (isset($resObj->error->errors[0])) {
                $msg .= " : " . $resObj->error->errors[0]->reason;
            }

            throw new \Exception($msg);
        } else {
            $itemsArray = $resObj->items;
            if (!is_array($itemsArray)) {
                return false;
            } else {
                return $itemsArray;
            }
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question