Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question