public function subscribChannel($username) { try { // Subscribe to a channel // Create a resource id with channel id and kind. // List Channels for the user $channels = $this->youtube->channels->listChannels('snippet', array("forUsername" => $username)); if (empty($channels) || !isset($channels['items'])) { return "Not Subscribed!"; } $channelId = $channels['items'][0]['id']; $resourceId = new Google_ResourceId(); $resourceId->setChannelId($channelId); $resourceId->setKind('youtube#channel'); // Create a snippet with resource id. $subscriptionSnippet = new Google_SubscriptionSnippet(); $subscriptionSnippet->setResourceId($resourceId); // Create a subscription request with snippet. $subscription = new Google_Subscription(); $subscription->setSnippet($subscriptionSnippet); // Execute the request and return an object containing information about the new subscription $subscriptionResponse = $this->youtube->subscriptions->insert('id,snippet', $subscription, array()); $htmlBody = "<h3>Subscription</h3><ul>"; $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']); $htmlBody .= '</ul>'; } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); return $htmlBody; } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); return $htmlBody; } $_SESSION['token'] = $this->client->getAccessToken(); return true; }
$_SESSION['token'] = $client->getAccessToken(); header('Location: ' . $redirect); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } // Check if access token successfully acquired if ($client->getAccessToken()) { try { // Subscribe to a channel // Create a resource id with channel id and kind. $resourceId = new Google_ResourceId(); $resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg'); $resourceId->setKind('youtube#channel'); // Create a snippet with resource id. $subscriptionSnippet = new Google_SubscriptionSnippet(); $subscriptionSnippet->setResourceId($resourceId); // Create a subscription request with snippet. $subscription = new Google_Subscription(); $subscription->setSnippet($subscriptionSnippet); // Execute the request and return an object containing information about the new subscription $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array()); $htmlBody .= "<h3>Subscription</h3><ul>"; $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']); $htmlBody .= '</ul>'; } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } $_SESSION['token'] = $client->getAccessToken();