Archive for December, 2013

Connect to Soundcloud API

1. Load Soundcloud Library


2.We have to specify  Client ID  Secret and Redirect URL sc_connect() method

public function sc_connect()
$config = array(
$soundcloud = new Services_Soundcloud($config);
$authorizeUrl = $soundcloud->getAuthorizeUrl();

CallBack to Soundcloud API

public function callback()

 $config = array(


 $soundcloud = new Services_Soundcloud($config);

 try {
 $accessToken = $soundcloud->accessToken($_GET['code']);

 } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
 try {
 $me = json_decode($soundcloud->get('me'), true);
 } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {

 echo "



Soundcloud API Library

Download Link : Soundcloud API

Soundcloud Library : Codeigniter  Folder Structure




Creating App

Step 1  : Register Your App


Step 2 : Form for App Details


App Name


Redirect URL (if your are developing Application in localhost use IP Address don’t use localhost)

Save Client ID and Secret

Step 3 : App Created


Step – 1 : Upload Video Metadata

  • API_XML_Request


<form action=”<?=base_url()?>index.php/controller/method” method=”post” >
<table style=”margin: 0 auto”>
<tr><td>Title : </td><td><input type=”text” name=”title” /></td></tr>
<tr><td>Description : </td><td><textarea name=”description”></textarea> </td></tr>
<tr><td></td><td><input type=”submit” value=”Upload”/></td></tr>

POST /action/GetUploadToken HTTP/1.1
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Content-Length: 1941255
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>
<entry xmlns=""
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    <media:keywords>toast, wedding</media:keywords>

Step 2 – Extracting values from the API response

When you submit an Upload API request, the API returns an XML response that contains an upload URL and upload token that enable the user to upload the actual video file. You will need to extract the URL and token from the response and include these values in the form on your web page where the user submits the video file.

The example below shows a sample API response to a request for browser-based uploading:

<?xml version='1.0' encoding='UTF-8'?>

Step 3 – Uploading the video file

After extracting the upload URL and upload token from the API response, you need to display a form so that the user can upload the actual video file. The form must use the upload URL as the value of the <form> tag’s action attribute and have a hidden input field containing the upload token. In addition, the form should verify that the user has selected a file to upload before allowing the user to actually submit the form.

<script type="text/javascript">
  function checkForFile() {
    if (document.getElementById('file').value) {
      return true;
    document.getElementById('errMsg').style.display = '';
    return false;

<form action="URL?" method="post"
  enctype="multipart/form-data" onsubmit="return checkForFile();">
  <input id="file" type="file" name="file"/>
  <div id="errMsg" style="display:none;color:red">
    You need to specify a file.
  <input type="hidden" name="token" value="TOKEN"/>
  <input type="submit" value="go" />

Step 4 – Codeigniter Method For Upload Video

public function browse_based_upload()
$params['apikey'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['key'] = '';
$params['oauth']['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['algorithm'] = 'HMAC-SHA1';
$params['oauth']['access_token'] = array('oauth_token'=>urlencode($this->session->userdata('oauth_token')),
$this->load->library('youtube', $params);
$title = $this->input->post('title'); // Video Title
$description = $this->input->post('description'); // Video Description
$privacy_setting = "public";
var_dump($privacy_setting); // Privacy Setting
if($privacy_setting == "public") // Public
$metadata = '
<entry xmlns="" xmlns:media="" xmlns:yt="">
<media:title type="plain">'.$title.'</media:title>
<media:description type="plain">'.$description.'</media:description>
<media:category scheme="">People</media:category>
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$ut['urltoken'] = array('url'=>$url,'token'=>$token);
else if($privacy_setting == "private")
 $metadata =
 '<entry xmlns="" xmlns:media="" xmlns:yt="" xmlns:gd="" gd:fields="media:group/yt:private">
 <media:title type="plain">'.$this->input->post('title').'</media:title>
 <media:description type="plain">'.$this->input->post('description').'</media:description>
 <media:category scheme="">People</media:category>
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$ut['urltoken'] = array('url'=>$url,'token'=>$token);
$this->load->view('redirect_viewname',$ut); } }

Step 5 – Important Steps

1. Need to add nexturl
2. You must set the value of the <form> tag's enctype attribute to multipart/form-data.
3. The <input> tag that identifies the file must be named file.
4. The <input> tag that contains the token must be named token.
5. Your application should verify that the user has selected a file to upload before allowing the user to submit the form to upload the file.

Step 6 – Process Flow Diagram


Step 7 – Process Flow Diagram

After Uploading Video , Return you will get Status and Video_id
1. Status = 200
2. Video_id = xxxxxxxxxxx

Refereed By : Google API’s

YouTube API Library for the CodeIgniter PHP Framework

The files for this project can be found on GitHub at:

Folder Structure

  • 1. google_oauth.php – project/application/libraries.
    This library retrieves an OAuth access token to authenticate with YouTube
  • 2. Youtube.php – project/application/libraries.
    This library interacts with the API to submit your requests and retrieve XML responses to those requests
  • 3. Oauth_helper.php – project/application/helpers.
    It signs and builds OAuth HTTP headers and is used internally by both library files.

2. Get an API key to access YouTube’s API. To obtain a key, go to

3. Establish OAuth Authentication from your new domain.

4. Configure Youtube API Key in Codeigniter

  • Step 1 : project/application/config.config.php
  • Step 2 : $config[‘youtube_api_key’] = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;

Refereed By : Google API’s

You need to register your application with Google to be able to use OAuth 2.0 authorization. The following steps explain how to register your application and create an OAuth 2.0 client ID to use in that application.

Create your project and select API services

Create a project in the Google APIs Console.

1. If you have not created a project before, the Console page displays a big Create project button.


2. If you have created a project before, you can create a new project via the Console menu shown below.


3. The console displays the Services pane, which shows a list of the Google API services that your application could use. Toggle the status for the YouTube Data API v3 service to ON. (You may need to read and accept a license agreement to complete this step for certain services.)

4. After enabling the services that your application will use, you can click on the API Access pane to retrieve your API key. API requests must specify an API key or an OAuth 2.0 token.


Create an OAuth 2.0 client ID

  1. Click the API Access pane in the navigation menu.


2. Click the Create an OAuth 2.0 client ID button.


3. Enter a product name for your application and, if you wish, a URL for your product logo. By default, we will use your email address as the support email for your application. You can change the email address by signing in to the API Console with a different account that has an email address that you have verified.


Click the Next button to proceed

4. Indicate whether your application is a web application or an installed application. If it is a web application, you will need to also provide a domain or hostname.


5. After you direct a user to Google’s authorization server, and the user logs in and decides whether to grant access to your application, Google will redirect the user to a redirect_uri that you specified.

  1. For web applications, you need to register the redirect_uri that your application uses with Google before you will be able to complete the steps needed to obtain OAuth 2.0 access tokens. You can register more than one redirect_uri.The Console will define a default redirect URI and a default, authorized JavaScript source. You can register additional redirect_uri values and add or change authorized JavaScript sources by clicking the more options link in the Client ID Settings window.
  2. Click the Create client ID button.

Refereed By : Google API’s