Zend Framework on Codeigniter: Youtube

youtube3
This post is about how to use Zend Framework on Codeigniter to show Youtube videos.

Download

Note:

The download file is set up for Windows. If you are not using Windows, then open application/helpers/zend_framework_helper.php and comment out Windows part and uncomment the other part.

<?php
// For Windows
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."contrib\\");
require_once ('Zend/Loader.php');

// For Non-Windows
/*
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/");
require_once 'Zend/Loader.php';
*/

?>

Implement Zend Framework in Codeigniter

First you have to implement Zend Framework in Codeigniter. Please read this post.

Controller/youtube2.php

Create a file and add the following code.

<?php
class Youtube2 extends Controller{
    protected $_youTube;

function __construct() {
    parent::Controller();
    Zend_Loader::loadClass('Zend_Gdata_YouTube');
    $this->_youTube = new Zend_Gdata_YouTube;
    }

function index(){
      // Gets playlists
    $data['playlistfeed']= $this->_youTube->getPlaylistListFeed('NationalGeographic');
    $this->load->view ('youtube_view2',$data);
    }

public function listAction(){
    // Gets playlist videos
    $playlistId = $this->uri->segment(4);
    $query = $this->_youTube->newVideoQuery(
    'http://gdata.youtube.com/feeds/playlists/' . $playlistId);
    $data['videoFeed']= $this->_youTube->getVideoFeed($query);
    $this->load->view('listaction_view', $data);
    }

public function viewAction(){
    // Gets video
    $videoId = $this->uri->segment(4);
    $data['videoId'] = $this->uri->segment(4);
    $data['videoEntry'] = $this->_youTube->getVideoEntry($videoId);
    $this->load->view('youtube_video_view', $data);
    }
}

Firstly I am adding a protected variable $_youTube.
In the constructor, I load the class Zend_Gdata_Youtube, and create a new object.

In the function index, I add a variable playlistfeed and send it to a view called youtube_view2 which I am going to create it shortly.

I am going to collect National geographic videos in this case. You can change it as you wish.

views/youtube_view2.php

Create a file and add the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Video lists</title>

</head>
<body>
<h1>National Geographic Videos on Youtube</h1>
<dl>
<?php foreach ($playlistfeed as $playlistEntry): >
<?php $id = substr(strrchr(
$playlistEntry->getPlaylistVideoFeedUrl(), '/'
), 1); >
<dt>
<a href="youtube2/listAction/id/<?php echo $id; >/">
<?php echo $playlistEntry->title->text; >
</a>
</dt>
<dd><?php echo $playlistEntry->description->text; ></dd>
<?php endforeach; >
</dl>

</body>
</html>

This will output the following html.

...
<h1>National Geographic Videos on Youtube</h1>
<dl>
<dt>
<a href="youtube2/listAction/id/746AD498C993B957/">
Oceans</a>

</dt>
<dd>Explore the beauty of the world's oceans.</dd>
<dt>
<a href="youtube2/listAction/id/355D512277EFFA06/">
Animal Attack!</a>
</dt>
...
...

When you click a link, it will take you listAction() function in youtube2 class and the fourth segment will be used as id in that class.
youtube1

views/listaction_view.php

Create a file and add the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Youtube Video Details</title>

</head>
<body>

<h1>National Geographic Videos on Youtube. Videos about <?php echo $videoFeed->title->text; ></h1>
<dl>
<?php foreach ($videoFeed as $videoEntry): >
<?php $id = substr(strrchr($videoEntry->getFlashPlayerUrl(), '/'),
1); >

<dt>
    <h2>
        <?php echo "Video Title: ". $videoEntry->mediaGroup->title->text; >

</h2>
   <h3> <?php echo anchor("youtube2/viewAction/id/$id/","Watch Video");>
</h3>

</dt>
<dd><?php echo $videoEntry->mediaGroup->description->text. "<br /><br />"; >
<b><?php echo "Video id: ". $id; >
</b>
</dd>
<?php endforeach; >
</dl>

</body>
</html>

This listAction() function outputs the following html.

...
<h1>National Geographic Videos on Youtube. Videos about Oceans</h1>
<dl>

<dt>
    <h2>
        Video Title: Feeding Frenzy: Manta Rays in the Maldives
</h2>
   <h3> <a href="http://127.0.0.1/ci_zend/index.php/youtube2/viewAction/id/yihoIvUBDM4?f=playlists&amp;amp;app=youtube_gdata">Watch Video</a></h3>

</dt>
<dd>When tide and current turn a tiny bay into a bowl of plankton, manta rays in the Maldives gather for a roiling, whirling feast.<br /><br /><b>Video id: yihoIvUBDM4?f=playlists&amp;amp;app=youtube_gdata</b>
</dd>

<dt>
    <h2>
        Video Title: Penguins vs. Pelicans
</h2>
   <h3> <a href="http://127.0.0.1/ci_zend/index.php/youtube2/viewAction/id/Fhmm-A8wBzk?f=playlists&amp;amp;app=youtube_gdata">Watch Video</a></h3>

</dt>
...
...

The links will take you to youtube2 viewAction () function with Youtube video identifier.
The viewAction() function will take the forth segment and show a video in the next step.

youtube2

views/youtube_video_view.php

Create a file and add the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Watch: Youtube Videos</title>

</head>
<body>

<h1><?php echo $videoEntry->mediaGroup->title->text; ></h1>
<object width="425" height="355">
<param name="movie"
value="http://www.youtube.com/v/<?php echo $videoId; >&amp;amp;rel=0">
</param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/<?php echo $videoId; >&amp;amp;rel=0"
type="application/x-shockwave-flash"
wmode="transparent" width="425" height="355">
</embed>
</object>

</body>
</html>

This view outputs the following html.

...
<h1>Feeding Frenzy: Manta Rays in the Maldives</h1>
<object width="425" height="355">
<param name="movie"
value="http://www.youtube.com/v/yihoIvUBDM4&amp;amp;rel=0">
</param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/yihoIvUBDM4&amp;amp;rel=0"
type="application/x-shockwave-flash"
wmode="transparent" width="425" height="355">
</embed>
</object>
...
...

2 comments to Zend Framework on Codeigniter: Youtube

  • harly

    A PHP Error was encountered

    Severity: Warning

    Message: domdocument::domdocument() expects at least 1 parameter, 0 given

    Filename: Gdata/App.php

    Line Number: 805

    i try in your code but trouble why?
    yesterday i try u code “How to implement zend framework with codeigniter” but it’s same trouble.

  • harly

    yesterday u say “December 11th, 2009 at 8:07 pm
    Try this. http://www.okadadesign.no/blog/?p=452

    i try but error it’s same.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>