
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
CREATE TABLE IF NOT EXISTS `d_videos` (
`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`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
<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') ?>"> <div> <div>
/* 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) {
$this->load->view('videos_new');
} 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
$this->videos_model->create_videos($video_id,$video_title);
}
}
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);
curl_close($ch);
if ($response) {
$result =json_decode($response, true);
return $result['title'];
} else {
return error_get_last();
}
}
Third step:
function create_videos($video_id,$video_title)
{
$this->db->set('vi_name',$video_title);
$this->db->set('vi_url',$video_id);
$this->db->set('vi_created', time());
$this->db->insert('d_videos');
return TRUE;
}
Last step:
show your videos data like this
($youtube_videos as $rv) { ?> <ul> (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> ul> } ?>
hope it will help you :)