Article Content:Get Youtube video title from video link in codeigniter

Get Youtube video title from video link in codeigniter

Get Youtube video title from video link in codeigniter.

If you want to add YouTube links to your website, you should know that google always changing their API which means that you must keep updating your code to match their updates.

YouTube now on the API3 which force you to create API key for every application, but the problem is if you want a simple solution without creating API key, this the tutorial came for, I suppose that you want to add your videos to the website and all you have YouTube video URL.


I think steps will be like this:               

1-     You will copy video link from YouTube channel paste it into your input box and click submit

2-     Your application will analyze link ,extract video code

3-     Call YouTube API3, give it  video code as parameter and it will return video data as JSON

4-     Your application takes the data and inserts it into a database table.

5-     Show video image and video title in your website

To do this let start:

First step:

Create your form input to add YouTube links and create your table schema



  `v_id` int(11) NOT NULL AUTO_INCREMENT,

  `v_name` varchar(150) CHARACTER SET utf8 NOT NULL,

  `v_url` varchar(255) CHARACTER SET utf8 NOT NULL,

  `v_created` int(11) NOT NULL,

  PRIMARY KEY (`v_id`)



<div class="form-group">
<label class="col-sm-4 control-label" for="vi_url">youtube linklabel>
<div class="col-sm-8">
<input type="text" placeholder="add link here" id="vi_url" class="form-control" name="vi_url" value="set_value('vi_url') ?>">

Second step:
Add two functions in your controller file Create function which fetches and validate data that comes from the input, get_youtube_title function which calls YouTube API, get the YouTube video title with YouTube video ID ,retrieve the response as JSON, and extract the only title.


/* This function create new videos */

function create() {

    $this->form_validation->set_rules('vi_url', $this->lang->line('vi_url'), 'trim|required|htmlspecialchars');

    if ($this->form_validation->run($this) == false) {
    } else {
        $url = $this->input->post('vi_url');
        parse_str(parse_url($url, PHP_URL_QUERY), $my_array_of_vars);
        // parse youtube url to get id
        $video_id = $my_array_of_vars['v'];
        // Output: AyLY1CYtB_8
        $video_title =  $this->get_youtube_title($video_id);
        // send id and video title to database table
function get_youtube_title($video_id){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='.$video_id.'&format=json');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($ch);

    if ($response) {
        $result =json_decode($response, true);
        return  $result['title'];
    } else {
        return error_get_last();


Important note:
you can Get All information from youtube video not only video title and we choose CURL because it is more safely than file_get_contents function; you can also change the last parameter in the link as XML if you want to retrieve data as XML.


Third step:

         add this function to your model file


function create_videos($video_id,$video_title)




    $this->db->set('vi_created', time());


    return TRUE;





Last step:

show your videos data like this


($youtube_videos as $rv) { ?>

   (isset($rv['vi_url']) and $rv['vi_url'] != "") { ?>
   <img src="http://img.youtube.com/vi/$rv['vi_url']; ?>/default.jpg" />
            } else { ?>
   <img alt="$rv['vi_name'] ?>" src="base_url(); ?>global/admin/images/no-img.png"  />
            } ?>  
   <p><a href="#">$rv['vi_name'] ?>a>p>
   <a title="category date" href="#" class="date">date("F j, Y", $rv['vi_created']) ?>a>

} ?>





hope it will help you :)

Leave a Reply

Your email address will not be published.

Notify me of followup comments via e-mail.
You can also Subscribe without commenting.