Hello i have a problem with my website. It is running slow. I asked the my server provider why is that, and they replied its because the webiste uses many embedded urls like youtube, vimeo, dailymotion and so on. And so i have to cache these embedded urls. Anyone knows how to do that?

I viewed this online. So all i have to do is put this code on my config page?

http://www.phpfastcache.com/

commented: Wow. Thanks for the link, Looks brilliant. Will be experiementing! +15
Member Avatar for diafol

Have you read the docs? Looks like an include file.

Yes i did it says that alla you have to do is include the files and put this on your config page. then on the end it says on comment // Output Your Contents $products HERE
what product contents? My config page has the connection for the database

and what about the other exambles?

I downloaded the files. On the examble.php

<?php
include("php_fast_cache.php");
phpFastCache::$storage = "auto";

// ready ?
// check in case first

$content = phpFastCache::get("keyword1");

if($content == null) {
    // for testing
    echo "This is not caching, page is render with lot queires and slow speed <br>";
    // do what you want, like get content from cURL | API | mySQL Query and return result to $content
    $content = file_get_contents("http://www.phpfastcache.com/testing.php");

    // rewrite cache for other request in 5 seconds
    phpFastCache::set("keyword1",$content,5);
} else {
    // use cache
    // node
    echo "THIS TIME USE CACHE, FAST! <br>";
}

echo "TRY F5 to refesh the page to see new SPEED with Cache!<br>";
echo $content;

why is this line?
$content = file_get_contents("http://www.phpfastcache.com/testing.php");

what should one put here?

I cant understand this.

// Try to get $products from Caching First
// product_page is "identity keyword";
$products = $cache->get("product_page");

What should i put for $products? What it means by identity keyword?

this

$content = file_get_contents("http://www.phpfastcache.com/testing.php");

get the remote page named testing.php from the phpfastcache.com by way of cURL, API or mySQL query.

for example if the testing.php returns

<!doctyp html>
<html>
<head>
</head>
<body>
    <h1>Hello world </h1>
</body> 

then the cache class will make a cache file of the page and generate a new file after 5 seconds as defined by the static method called set.

phpFastCache::set("keyword1",$content,5);

keyword1 is something that you need to define. So, if we call the static method set like this

phpFastCache::set("helloword",$content,5);

the cache file created by the class will be helloworld.

You have mentioned earlier about external urls from youtube,vimeo, dailymotion and so forth. The problem here is that even with the cache file generated, the url remains external. Meaning, the browser viewing the cached or non-cached page will still have to work hard in picking those embedded url.

What I am trying to tell you is that caching your content will not make sense, because the embedded url are external. However, it will only make sense if and only if the url are stored in the database. This will save you some loading time equal to the time that your server will send and return the query results.

If you are generating the url from the database, the resulting data can be represented like this. Let say we have a page called search.php with the following codes..

 //function get_data()
 //{
      $data = array(
                'youtube' => 'some_item_from_youtube',
                'vimeo' => 'some_item_from_vimeo',
                'dailymotion' => 'some_item_from_dailymotion'
              );

     if(isset($_GET) && (!empty($_GET['q']))){
             foreach($data as $k => $v){
                 echo $k .' : '. $v .'<br />';
                 }
       }

       else{
             //return false;

             false;

           }
    //}       

You can execute the cache class like this

 $content = file_get_contents("http://YourDomain.com/search.php?q=videolink");

 phpFastCache::set("videolink",$content,5);

check if the cached videolink exists

$products = $cache->get("videolink");

you will have an output similar to this

youtube : some_item_from_youtube
vimeo : some_item_from_vimeo
dailymotion : some_item_from_dailymotion

a new cache file will be regenerated after 5 seconds.

Ok i think i got it, but i have a couple of questions.

Yes the urls are stored in the database so i guess that why they told me that.

Let me start from an easy question.

     $content = file_get_contents("http://MyDomain.com/the_file_below.php?q=videolink");
    phpFastCache::set("videolink",$content,5);

These lines go the config.php right? or in whatever file i make the connection with the database.

The .php file that calls all the embedded urls is this

private $all_types = array(
        "youtube" => array(
            "link" => "/https?:\/\/[w\.]*youtube\.com\/watch\?v=([^&#]*)|https?:\/\/[w\.]*youtu\.be\/([^&#]*)/i",
            "embed" => '/https?:\/\/[w\.]*youtube\.com\/v\/([^?&#"\']*)/is',
            "iframe" => '/https?:\/\/[w\.]*youtube\.com\/embed\/([^?&#"\']*)/is'
        ),
        "vimeo" => array(
            "link" => "/https?:\/\/[w\.]*vimeo\.com\/([\d]*)/is",
            "embed" => '/https?:\/\/[w\.]*vimeo\.com\/moogaloop\.swf\?clip_id=([\d]*)/is',
            "iframe" => '/https?:\/\/player\.vimeo\.com\/video\/([\d]*)/is'
        ),

        "dailymotion" => array(
            "link" => "/https?:\/\/[w\.]*dailymotion\.com\/video\/([^_]*)/is",
            "embed" => '/https?:\/\/[w\.]*dailymotion\.com\/swf\/video\/([^?&#"\']*)/is',
            "iframe" => '/https?:\/\/[w\.]*dailymotion\.com\/embed\/video\/([^?&#"\']*)/is'
        ),

        "metacafe" => array(
            "link" => '/https?:\/\/[w\.]*metacafe\.com\/watch\/([^?&#"\']*)/is',
            "embed" => '/https?:\/\/[w\.]*metacafe\.com\/fplayer\/(.*).swf/is'
        ),


        "clickthrough" => array(
            "link" => '/https?:\/\/[w\.]*clikthrough\.com\/theater\/video\/([\d]*)/is',
            "embed" => '/https?:\/\/[w\.]*clikthrough\.com\/clikPlayer\.swf\?videoId=([\d]*)/is'
        )
        ,
        "dotsub" => array(
            "link" => '/https?:\/\/[w\.]*dotsub\.com\/view\/([^\?\/&#]*)/is',
            "iframe" => '/https?:\/\/[w\.]*dotsub\.com\/media\/(.*)\/e/is'
        ),
        "revision" => array(
            "link" => '/https?:\/\/[w\.]*revision3\.com\/([^?&#"\']*)/is',
        ),
        "videojug" => array(
            "link" => '/https?:\/\/[w\.]*videojug\.com\/film\/([^?]*)/is',
        ),
        "blip" => array(
            "link" => '/https?:\/\/[w\.]*blip\.tv\/([^?]*)/is',
        ),

        "screenr" => array(
            "link" => '/https?:\/\/[w\.]*screenr\.com\/([^?]*)/is',
        ),
        "slideshare" => array(
            "link" => '/https?:\/\/[w\.]*slideshare\.net\/([^?]*)/is',
        ),
        "hulu" => array(
            "link" => '/https?:\/\/[w\.]*hulu\.com\/watch\/([^?]*)/is',
        ),

        "flickr" => array(
            "link" => '/https?:\/\/[w\.]*flickr\.com\/photos\/([^?]*)/is',
        ),
        "funnyordie" => array(
            "link" => '/https?:\/\/[w\.]*funnyordie\.com\/videos\/([^?]*)/is',
        ),
        "twitpic" => array(
            "link" => '/https?:\/\/[w\.]*twitpic\.com\/([^?]*)/is',
        ),

        "imgur" => array(
            "link" => '/https?:\/\/[w\.]*imgur\.[^\/]*\/([^?]*)/is',
        ),
        "deviantart" => array(
                "link" => '/https?:\/\/[^\/]*\.*deviantart\.[^\/]*\/([^?]*)/is',
        ),
        "soundcloud" => array(
            "link" => '/https?:\/\/[w\.]*soundcloud\.[^\/]*\/([^?]*)/is',
        ),
        "instagram" => array(
            "link" => '/https?:\/\/[w\.]*instagram\.[^\/]*\/([^?]*)/is',
        ),
        "kickstarter" => array(
                "link" => '/https?:\/\/[w\.]*kickstarter\.[^\/]*\/([^?]*)/is',
        ),
        "ted" => array(
            "link" => '/https?:\/\/[w\.]*ted\.[^\/]*\/([^?]*)/is',
        )
    );

so after this i write,

 if(isset($_GET) && (!empty($_GET['q']))){
foreach($all_types as $k => $v){
echo $k .' : '. $v .'<br />';
}
}
else{
//return false;
false;
}

Am i right?

this is alla the code from the file that handles embedded urls

<?php

class Wall_Expand
{
    private $code = "";
    private $site = "";
    private $data = array(
        "small" => "", 
        "medium" => "", 
        "large" => "", 
        "w" => -1, 
        "h" => -1, 
        "embed" => "",
        "iframe" => "",
        "url" => "",
        "site" => "", 
        "title" => ""
        );
    private $default_size = array("w" => 472, "h" => 268);
    private $default_size_tiny = array("w" => 230, "h" => 170);
    private $all_types = array(
        "youtube" => array(
            "link" => "/https?:\/\/[w\.]*youtube\.com\/watch\?v=([^&#]*)|https?:\/\/[w\.]*youtu\.be\/([^&#]*)/i",
            "embed" => '/https?:\/\/[w\.]*youtube\.com\/v\/([^?&#"\']*)/is',
            "iframe" => '/https?:\/\/[w\.]*youtube\.com\/embed\/([^?&#"\']*)/is'
        ),
        "vimeo" => array(
            "link" => "/https?:\/\/[w\.]*vimeo\.com\/([\d]*)/is",
            "embed" => '/https?:\/\/[w\.]*vimeo\.com\/moogaloop\.swf\?clip_id=([\d]*)/is',
            "iframe" => '/https?:\/\/player\.vimeo\.com\/video\/([\d]*)/is'
        ),

        "dailymotion" => array(
            "link" => "/https?:\/\/[w\.]*dailymotion\.com\/video\/([^_]*)/is",
            "embed" => '/https?:\/\/[w\.]*dailymotion\.com\/swf\/video\/([^?&#"\']*)/is',
            "iframe" => '/https?:\/\/[w\.]*dailymotion\.com\/embed\/video\/([^?&#"\']*)/is'
        ),

        "metacafe" => array(
            "link" => '/https?:\/\/[w\.]*metacafe\.com\/watch\/([^?&#"\']*)/is',
            "embed" => '/https?:\/\/[w\.]*metacafe\.com\/fplayer\/(.*).swf/is'
        ),


        "clickthrough" => array(
            "link" => '/https?:\/\/[w\.]*clikthrough\.com\/theater\/video\/([\d]*)/is',
            "embed" => '/https?:\/\/[w\.]*clikthrough\.com\/clikPlayer\.swf\?videoId=([\d]*)/is'
        )
        ,
        "dotsub" => array(
            "link" => '/https?:\/\/[w\.]*dotsub\.com\/view\/([^\?\/&#]*)/is',
            "iframe" => '/https?:\/\/[w\.]*dotsub\.com\/media\/(.*)\/e/is'
        ),
        "revision" => array(
            "link" => '/https?:\/\/[w\.]*revision3\.com\/([^?&#"\']*)/is',
        ),
        "videojug" => array(
            "link" => '/https?:\/\/[w\.]*videojug\.com\/film\/([^?]*)/is',
        ),
        "blip" => array(
            "link" => '/https?:\/\/[w\.]*blip\.tv\/([^?]*)/is',
        ),

        "screenr" => array(
            "link" => '/https?:\/\/[w\.]*screenr\.com\/([^?]*)/is',
        ),
        "slideshare" => array(
            "link" => '/https?:\/\/[w\.]*slideshare\.net\/([^?]*)/is',
        ),
        "hulu" => array(
            "link" => '/https?:\/\/[w\.]*hulu\.com\/watch\/([^?]*)/is',
        ),

        "flickr" => array(
            "link" => '/https?:\/\/[w\.]*flickr\.com\/photos\/([^?]*)/is',
        ),
        "funnyordie" => array(
            "link" => '/https?:\/\/[w\.]*funnyordie\.com\/videos\/([^?]*)/is',
        ),
        "twitpic" => array(
            "link" => '/https?:\/\/[w\.]*twitpic\.com\/([^?]*)/is',
        ),

        "imgur" => array(
            "link" => '/https?:\/\/[w\.]*imgur\.[^\/]*\/([^?]*)/is',
        ),
        "deviantart" => array(
                "link" => '/https?:\/\/[^\/]*\.*deviantart\.[^\/]*\/([^?]*)/is',
        ),
        "soundcloud" => array(
            "link" => '/https?:\/\/[w\.]*soundcloud\.[^\/]*\/([^?]*)/is',
        ),
        "instagram" => array(
            "link" => '/https?:\/\/[w\.]*instagram\.[^\/]*\/([^?]*)/is',
        ),
        "kickstarter" => array(
                "link" => '/https?:\/\/[w\.]*kickstarter\.[^\/]*\/([^?]*)/is',
        ),
        "ted" => array(
            "link" => '/https?:\/\/[w\.]*ted\.[^\/]*\/([^?]*)/is',
        )
    );

    function __construct($input){
        foreach($this->all_types as $site => $types)
        {
            foreach($types as $type => $regexp)
            {
                preg_match($regexp, $input, $match);
                if(!empty($match))
                {
                    /*echo "<p>".$site." ".$type."</p>";
                    echo "<pre>";
                    print_r($match);
                    echo "</pre>";*/
                    for($i = 1; $i < sizeof($match); $i++)
                    {
                        if($match[$i] != "")
                        {
                            $this->code = $match[$i];
                            $this->site = $site;
                            break;
                        }
                    }
                    if($this->code != "")
                    {
                        break;
                    }
                }
            }
            if($this->code != "")
            {
                break;
            }
        }
    }

    /**************************
    * PUBLIC FUNCTIONS
    **************************/

    public function get_thumb($size = "small"){
        if($this->site != "")
        {
            $size_types = array("small", "medium", "large");
            $size = strtolower($size);
            if(!in_array($size, $size_types))
            {
                $size = "small";
            }
            $this->prepare_data("thumb");
            return $this->data[$size];
        }
        else
        {
            return "";
        }
    }

    public function get_iframe($w = -1, $h = -1){
        $this->prepare_data("iframe");
        if($this->site != "" && $this->data["iframe"] != "")
        {

            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size["h"];
            }
            $w=470;
            return '<iframe class="new" width="'.$w.'" height="'.$h.'" src="'.$this->data["iframe"].'" frameborder="0" allowfullscreen></iframe>';
        }
        else
        {
            return "";
        }
    }

    public function get_iframe_tiny($w = -1, $h = -1){
        $this->prepare_data("iframe");
        if($this->site != "" && $this->data["iframe"] != "")
        {

            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size_tiny["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size_tiny["h"];
            }
            $w=230;
            return '<iframe class="new" width="'.$w.'" height="'.$h.'" src="'.$this->data["iframe"].'?autoplay=1" frameborder="0" allowfullscreen></iframe>';
        }
        else
        {
            return "";
        }
    }

    public function get_embed($w = -1, $h = -1){
        $this->prepare_data("embed");
        if($this->site != "" && $this->data["embed"])
        {
            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size["h"];
            }
            return '<object width="'.$w.'" height="'.$h.'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="'.$this->data["embed"].'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'.$this->data["embed"].'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$w.'" height="'.$h.'"></embed></object>';
        }
        else
        {
            return "";
        }
    }

    public function get_url(){
        if($this->site != "")
        {
            $this->prepare_data("url");
            return $this->data["url"];
        }
        else
        {
            return "";
        }
    }

    public function get_id(){
        return $this->code;
    }

    public function get_site(){
        $this->prepare_data("site");
        return $this->data["site"];
    }

    public function get_size(){
        $arr = array();
        $this->prepare_data("size");
        $arr["w"] = ($this->data["w"] < 0) ? $this->default_size["w"] : $this->data["w"];
        $arr["h"] = ($this->data["h"] < 0) ? $this->default_size["h"] : $this->data["h"];
        return $arr;
    }

    public function get_title(){
        $this->prepare_data("title");
        return $this->data["title"];
    }

    /**************************
    * PRIVATE FUNCTIONS
    **************************/
    private function get_data($url){
        //echo "<p>Curl request ".$url."</p>";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
        $curlData = curl_exec($curl);
        curl_close($curl);
        return $curlData;
    }

    private function prepare_data($type){
        if($this->site != "")
        {
            $ready = false;
            switch($type)
            {
                case "size":
                    if($this->data["w"] > 0 && $this->data["h"] > 0)
                    {
                        $ready = true;
                    }
                break;
                case "thumb":
                    if($this->data["small"] != "" && $this->data["medium"] != "" && $this->data["large"] != "")
                    {
                        $ready = true;
                    }
                break;
                default:
                if($this->data[$type] != "")
                {
                    $ready = true;
                }
            }
            //if information is not yet loaded
            if(!$ready)
            {
                $func = ($this->site)."_data";
                $arr = $this->$func();
                //check if information requires http request
                if(!$arr[$type])
                {
                    //if not, just provide data
                    $func = ($this->site)."_".$type;
                    $this->aggregate($this->$func(), $type);
                }
                else
                {
                    //else if it needs http request we may as well load all other data
                    //so we won't need to request it again
                    $req = ($this->site)."_req";
                    $res = $this->get_data($this->$req());
                    foreach($arr as $key => $val)
                    {
                        $func = ($this->site)."_".$key;
                        if($val)
                        {
                            $this->aggregate($this->$func($res), $key);
                        }
                        else
                        {
                            $this->aggregate($this->$func(), $key);
                        }
                    }
                }
            }
        }
    }

    private function aggregate($data, $type){
        if(is_array($data))
        {
            foreach($data as $key => $val)
            {
                $this->data[$key] = $val;
            }
        }
        else
        {
            $this->data[$type] = $data;
        }
    }

    /**************************
    * SOME STANDARDS
    **************************/
    //oembed functions
    private function oembed_size($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res) && isset($res["width"]) && isset($res["height"]))
        {
            $arr["w"] = (int)$res["width"];
            $arr["h"] = (int)$res["height"];
        }
        return $arr;
    }

    private function oembed_title($res){
        $title = "";
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res) && isset($res["title"]))
        {
            $title = $res["title"];
        }
        return $title;
    }

    //og functions
    private function og_size($res){
        $arr = array();
        preg_match( '/property="og:video:width"\s*content="([\d]*)/i', $res, $match);
        if(!empty($match))
        {
            $arr["w"] = (int)$match[1];
        }
        preg_match( '/property="og:video:height"\s*content="([\d]*)/i', $res, $match);
        if(!empty($match))
        {
            $arr["h"] = (int)$match[1];
        }
        return $arr;
    }

    private function og_title($res){
        $ret = "";
        preg_match( '/property="og:title"\s*content="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {
            $ret = $match[1];
        }
        return $ret;
    }

    private function og_video($res){
        $code = "";
        preg_match( '/<meta\s*property="og:video"\s*content="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {
            $code = $match[1];
        }
        return $code;
    }

    //others
    private function link2title(){
        $title = "";
        $parts = explode("/", $this->code);
        if(isset($parts[1]))
        {
            $parts = explode("_", $parts[1]);
            foreach($parts as $key => $val)
            {
                $parts[$key] = ucfirst($val);
            }
            $title = implode(" ", $parts);
        }
        return $title;
    }
    /**************************
    * YOUTUBE FUNCTIONS
    **************************/

    //which data needs additional http request
    private function youtube_data(){
        return  array(
            "thumb" => false, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function youtube_req(){
        return $this->youtube_url();
    }
    //return thumbnails
    private function youtube_thumb(){
        $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
        $arr = array();
        foreach($size_types as $key => $val)
        {
            $arr[$key] = "http://i.ytimg.com/vi/".($this->code)."/".$val.".jpg";
        }
        return $arr;
    }
    //return size
    private function youtube_size($res){
        return $this->og_size($res);
    }
    //return iframe url
    private function youtube_iframe(){
        return "http://www.youtube.com/embed/".($this->code);
    }
    //return embed url
    private function youtube_embed(){
        return "http://www.youtube.com/v/".($this->code);
    }
    //return canonical url
    private function youtube_url(){
        return "http://www.youtube.com/watch?v=".($this->code);
    }
    //return website url
    private function youtube_site(){
        return "http://www.youtube.com";
    }
    //return title
    private function youtube_title($res){
        return $this->og_title($res);
    }

    /**************************
    * IMGUR FUNCTIONS
    **************************/
    private function imgur_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }

    private function imgur_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['url'];
        }
        return $arr;
    }

    private function imgur_req(){
        return "http://api.imgur.com/oembed/?url=".$this->imgur_url();
    }

    private function imgur_url(){
        return "http://imgur.com/gallery/".($this->code);
    }

    //return size
    private function imgur_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function imgur_iframe(){
        return "";
    }
    //return embed url
    private function imgur_embed(){
        return "";
    }

    //return website url
    private function imgur_site(){
        return "http://www.imgur.com";
    }
    //return title
    private function imgur_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * deviantart FUNCTIONS
    **************************/
    private function deviantart_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }

    private function deviantart_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['url'];
        }
        return $arr;
    }

    private function deviantart_req(){
        return "http://backend.deviantart.com/oembed?format=json&url=".$this->deviantart_url();
    }

    private function deviantart_url(){
        return "http://deviantart.com/".($this->code);
    }

    //return size
    private function deviantart_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function deviantart_iframe(){
        return "";
    }
    //return embed url
    private function deviantart_embed(){
        return "";
    }

    //return website url
    private function deviantart_site(){
        return "http://www.deviantart.com";
    }
    //return title
    private function deviantart_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * SOUNDCLOUD FUNCTIONS
    **************************/
    private function soundcloud_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => true,
            "url" => true,
            "site" => false, 
            "title" => true
        );
    }

    private function soundcloud_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['thumbnail_url'];
        }
        return $arr;
    }

    private function soundcloud_req(){      
        return "http://soundcloud.com/oembed?format=json&url=".$this->soundcloud_url();
    }

    private function soundcloud_embed(){
        return "";
    }

    private function soundcloud_size($res){
        return array("w" => "100%", "h" => 166);
    }

    private function soundcloud_iframe($res){
        $data = json_decode($res);
        $array = array();
        preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;
        return $array[1];
    }

    private function soundcloud_site(){
        return "http://www.soundcloud.com";
    }

    private function soundcloud_url(){
        return "http://soundcloud.com/".($this->code);
    }

    private function soundcloud_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * kickstarter FUNCTIONS
    **************************/
    private function kickstarter_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => true,
            "url" => true,
            "site" => false, 
            "title" => true
        );
    }

    private function kickstarter_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['thumbnail_url'];
        }
        return $arr;
    }

    private function kickstarter_req(){ 
    //  echo "http://www.kickstarter.com/services/oembed?url=".urlencode($this->kickstarter_url());
        return "http://www.kickstarter.com/services/oembed?url=".urlencode($this->kickstarter_url());
    }

    private function kickstarter_embed(){
        return "";
    }

    private function kickstarter_size($res){
        return array("w" => "100%");
    }

    private function kickstarter_iframe($res){
        $data = json_decode($res);

        $array = array();
        preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;

        return $array[1];
    }

    private function kickstarter_site(){
        return "http://www.kickstarter.com";
    }

    private function kickstarter_url(){
        return "http://www.kickstarter.com/".($this->code);
    }

    private function kickstarter_title($res){
        return $this->oembed_title($res);
    }





    /**************************
    * INSTAGRAM FUNCTIONS
    **************************/
    private function instagram_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => true,
            "site" => false, 
            "title" => true
        );
    }

    private function instagram_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['url'];
        }
        return $arr;
    }

    private function instagram_req(){
        return "http://api.instagram.com/oembed?url=".$this->instagram_url();
    }

    private function instagram_embed(){
        return "";
    }

    private function instagram_iframe(){
        return "";
    }

    private function instagram_size($res){
        return $this->oembed_size($res);
    }

    private function instagram_site(){
        return "http://www.instagram.com";
    }

    private function instagram_url(){
        return "http://instagram.com/".($this->code);
    }

    private function instagram_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * TED FUNCTIONS
    **************************/
    private function ted_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => true,
            "url" => true,
            "site" => false, 
            "title" => true
        );
    }

    private function ted_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            //echo $res = current($res); exit;
            $arr['medium'] = $res['thumbnail_url'];
        }
        return $arr;
    }

    private function ted_req(){     

        return "http://www.ted.com/talks/oembed.json?url=".$this->ted_url();
    }

    private function ted_embed(){
        return "";
    }


    private function ted_size($res){
        return array("w" => "100%");
    }

    private function ted_iframe($res){

        $data = json_decode($res);

        $array = array();
        preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;

        return $array[1];
    }

    private function ted_site(){
        return "http://www.ted.com";
    }

    private function ted_url(){
        return "http://www.ted.com/".($this->code);
    }

    private function ted_title($res){
        return $this->oembed_title($res);
    }


    /**************************
    * VIMEO FUNCTIONS
    **************************/

    //which data needs additional http request
    private function vimeo_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function vimeo_req(){
        return "http://vimeo.com/api/v2/video/".($this->code).".json";
    }
    //return thumbnails
    private function vimeo_thumb($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            $res = current($res);
            $sizes = array("small", "medium", "large");
            foreach($sizes as $val)
            {
                $arr[$val] = $res["thumbnail_".$val];
            }
        }
        return $arr;
    }
    //return size
    private function vimeo_size($res){
        $res = json_decode($res, true);
        $arr = array();
        if(is_array($res) && !empty($res))
        {
            $res = current($res);
            $arr["w"] = (int)$res["width"];
            $arr["h"] = (int)$res["height"];
        }
        return $arr;
    }
    //return iframe link
    private function vimeo_iframe(){
        return "http://player.vimeo.com/video/".($this->code);
    }
    //return embed url
    private function vimeo_embed(){
        return "http://vimeo.com/moogaloop.swf?clip_id=".($this->code);
    }
    //return canonical url
    private function vimeo_url(){
        return "http://www.vimeo.com/".($this->code);
    }
    //return website url
    private function vimeo_site(){
        return "http://www.vimeo.com";
    }
    //return title
    private function vimeo_title($res){
        $res = json_decode($res, true);
        $title = "";
        if(is_array($res) && !empty($res))
        {
            $res = current($res);
            $title = $res["title"];
        }
        return $title;
    }



    /**************************
    * DAILYMOTION FUNCTIONS
    **************************/
    //which data needs additional http request
    private function dailymotion_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function dailymotion_req(){
        return "http://www.dailymotion.com/services/oembed?format=json&url=".$this->dailymotion_url();
    }
    //return thumbnails
    private function dailymotion_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["large"] = $res["thumbnail_url"];
            $arr["medium"] = str_replace("large", "medium", $res["thumbnail_url"]);
            $arr["small"] = str_replace("large", "small", $res["thumbnail_url"]);
        }
        return $arr;
    }
    //return size
    private function dailymotion_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function dailymotion_iframe(){
        return "http://www.dailymotion.com/embed/video/".($this->code);
    }
    //return embed url
    private function dailymotion_embed(){
        return "http://www.dailymotion.com/swf/video/".($this->code);
    }
    //return canonical url
    private function dailymotion_url(){
        return "http://www.dailymotion.com/video/".($this->code);
    }
    //return website url
    private function dailymotion_site(){
        return "http://www.dailymotion.com";
    }
    //return title
    private function dailymotion_title($res){
        return $this->oembed_title($res);
    }


    /**************************
    * METACAFE FUNCTIONS
    **************************/
    //which data needs additional http request
    private function metacafe_data(){
        return array(
            "thumb" => false, 
            "size" => false, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => false
        );
    }
    //return http request url where to get data
    private function metacafe_req(){
        return "";
    }
    //return thumbnails
    private function metacafe_thumb(){
        $arr = array();
        $parts = explode("/", $this->code);
        $arr["medium"] = "http://s.mcstatic.com/thumb/".$parts[0].".jpg";
        $arr["large"] = "http://s.mcstatic.com/thumb/".$parts[0]."/0/4/videos/0/1/".$parts[1].".jpg";
        $arr["small"] = "http://s.mcstatic.com/thumb/".$parts[0]."/0/4/sidebar_16x9/0/1/".$parts[1].".jpg";
        return $arr;
    }
        //return size
    private function metacafe_size(){
        $arr = array();
        $arr["w"] = 460;
        $arr["h"] = 284;
        return $arr;
    }
    //return iframe url
    private function metacafe_iframe(){
        $code = ($this->code[strlen($this->code)-1] == "/") ? substr($this->code, 0, strlen($this->code)-1) : $this->code;
        return "http://www.metacafe.com/fplayer/".$code.".swf";
    }
    //return embed url
    private function metacafe_embed(){
        return $this->metacafe_iframe();
    }
    //return canonical url
    private function metacafe_url(){
        $code = ($this->code[strlen($this->code)-1] != "/") ? ($this->code)."/" : $this->code;
        return "http://www.metacafe.com/watch/".($code);
    }
    //return website url
    private function metacafe_site(){
        return "http://www.metacafe.com";
    }
    //return title
    private function metacafe_title(){
        return $this->link2title();
    }



    /**************************
    * CLICKTHROUGH FUNCTIONS
    **************************/
    //which data needs additional http request
    private function clickthrough_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function clickthrough_req(){
        return "http://www.clikthrough.com/services/oembed/?url=".$this->clickthrough_url()."%26format%3Djson";
    }
    //return thumbnails
    private function clickthrough_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["medium"] = $res["thumbnail_url"];
            $arr["small"] = str_replace("/M-", "/Sw-", $arr["medium"]);
            $arr["large"] = str_replace("/M-", "/L-", $arr["medium"]);
        }
        return $arr;
    }
    //return size
    private function clickthrough_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function clickthrough_iframe(){
        return "http://www.clikthrough.com/clikPlayer.swf?videoId=".$this->code;
    }
    //return embed url
    private function clickthrough_embed(){
        return $this->clickthrough_iframe();
    }
    //return canonical url
    private function clickthrough_url(){
        return "http://www.clikthrough.com/theater/video/".($this->code);
    }
    //return website url
    private function clickthrough_site(){
        return "http://www.clikthrough.com";
    }
    //return title
    private function clickthrough_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * DOTSUB FUNCTIONS
    **************************/
    //which data needs additional http request
    private function dotsub_data(){
        return array(
            "thumb" => false, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function dotsub_req(){
        return "http://dotsub.com/services/oembed?url=".$this->dotsub_url();
    }
    //return thumbnails
    private function dotsub_thumb(){
        $arr = array();
        $arr["medium"] = "http://dotsub.com/media/".$this->code."/t";
        $arr["small"] = "http://dotsub.com/media/".$this->code."/t";
        $arr["large"] = "http://dotsub.com/media/".$this->code."/t";
        return $arr;
    }
    //return size
    private function dotsub_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function dotsub_iframe(){
        return "http://dotsub.com/static/players/portalplayer.swf?uuid=".($this->code)."&lang=eng&plugins=dotsub&embedded=true";
    }
    //return embed url
    private function dotsub_embed(){
        return $this->dotsub_iframe();
    }
    //return canonical url
    private function dotsub_url(){
        return "http://dotsub.com/view/".($this->code);
    }
    //return website url
    private function dotsub_site(){
        return "http://www.dotsub.com";
    }
    //return title
    private function dotsub_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * REVISION3 FUNCTIONS
    **************************/
    //which data needs additional http request
    private function revision_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => true,
            "iframe" => true,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function revision_req(){
        return $this->revision_url();
    }
    //return thumbnails
    private function revision_thumb($res){
        $arr = array();
        preg_match( '/<link\s*rel="image_src"\s*href="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {

            $arr["small"] = $match[1];
            $arr["medium"] = str_replace("small.thumb.jpg", "medium.thumb.jpg", $match[1]);
            $arr["large"] = str_replace("small.thumb.jpg", "large.thumb.jpg", $match[1]);
        }
        return $arr;
    }
    //return size
    private function revision_size($res){
        return $this->og_size($res);
    }
    //return iframe url
    private function revision_iframe($res){
        return $this->og_video($res);
    }
    //return embed url
    private function revision_embed($res){
        return $this->revision_iframe($res);
    }
    //return canonical url
    private function revision_url(){
        return "http://revision3.com/".($this->code);
    }
    //return website url
    private function revision_site(){
        return "http://www.revision3.com";
    }
    //return title
    private function revision_title($res){
        return $this->og_title($res);
    }

    /**************************
    * VIDEOJUG FUNCTIONS
    **************************/
    //which data needs additional http request
    private function videojug_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => true,
            "iframe" => true,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function videojug_req(){
        return $this->videojug_url();
    }
    //return thumbnails
    private function videojug_thumb($res){
        $arr = array();
        preg_match( '/<link\s*rel="image_src"\s*href="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {

            $arr["medium"] = $match[1];
            $arr["small"] = str_replace("Medium.jpg", "Small.jpg", $match[1]);
            $arr["large"] = str_replace("Medium.jpg", "Large.jpg", $match[1]);
        }
        return $arr;
    }
    //return size
    private function videojug_size($res){
        $arr = array();
        preg_match( '/<meta\s*name="video_height"\s*content="([\d]*)"/i', $res, $match);
        if(!empty($match))
        {
            $arr["h"] = (int)$match[1];
        }
        preg_match( '/<meta\s*name="video_width"\s*content="([\d]*)"/i', $res, $match);
        if(!empty($match))
        {
            $arr["w"] = (int)$match[1];
        }
        return $arr;
    }
    //return iframe url
    private function videojug_iframe($res){
        $code = "";
        preg_match( '/<link\s*href="([^"]*)"\s*rel="video_src"\s*\/>/i', $res, $match);
        if(!empty($match))
        {
            $code = $match[1];
        }
        return $code;
    }
    //return embed url
    private function videojug_embed($res){
        return $this->videojug_iframe($res);
    }
    //return canonical url
    private function videojug_url(){
        return "http://www.videojug.com/film/".($this->code);
    }
    //return website url
    private function videojug_site(){
        return "http://www.videojug.com";
    }
    //return title
    private function videojug_title($res){
        $title = "";
        preg_match( '/<meta\s*name="title"\s*content="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {
            $title =$match[1];
        }
        return $title;
    }

    /**************************
    * BLIP FUNCTIONS
    **************************/
    //which data needs additional http request
    private function blip_data(){
        return array(
            "thumb" => true, 
            "size" => false, 
            "embed" => true,
            "iframe" => true,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function blip_req(){
        return $this->blip_url();
    }
    //return thumbnails
    private function blip_thumb($res){
        $arr = array();
        preg_match( '/<meta\s*property="og:image"\s*content="([^"]*)"/i', $res, $match);
        if(!empty($match))
        {

            $arr["large"] = $match[1];
            $file = explode("blip.tv/", $match[1]);
            $arr["small"] = "http://i.blip.tv/g?src=".$file[1]."&w=140&h=80";
            $arr["medium"] = "http://i.blip.tv/g?src=".$file[1]."&w=300&h=170";
        }
        return $arr;
    }
    //return size
    private function blip_size(){
        $arr = array();
        $arr["h"] = 450;
        $arr["w"] = 800;
        return $arr;
    }
    //return iframe url
    private function blip_iframe($res){
        return $this->og_video($res);
    }
    //return embed url
    private function blip_embed($res){
        return $this->blip_iframe($res);
    }
    //return canonical url
    private function blip_url(){
        return "http://blip.tv/".($this->code);
    }
    //return website url
    private function blip_site(){
        return "http://blip.tv";
    }
    //return title
    private function blip_title($res){
        return $this->og_title($res);
    }


    /**************************
    * SCREENR FUNCTIONS
    **************************/
    //which data needs additional http request
    private function screenr_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function screenr_req(){
        return "http://www.screenr.com/api/oembed.json?url=".$this->screenr_url();
    }
    //return thumbnails
    private function screenr_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["small"] = $res["thumbnail_url"];
            $arr["medium"] = $res["thumbnail_url"];
            $arr["large"] = str_replace("_thumb", "", $res["thumbnail_url"]);
        }
        return $arr;
    }
    //return size
    private function screenr_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function screenr_iframe(){
        return "http://www.screenr.com/embed/".($this->code);
    }
    //return embed url
    private function screenr_embed(){
        return "";
    }
    //return canonical url
    private function screenr_url(){
        return "http://www.screenr.com/".($this->code);
    }
    //return website url
    private function screenr_site(){
        return "http://www.screenr.com";
    }
    //return title
    private function screenr_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * SLIDESHARE FUNCTIONS
    **************************/
    //which data needs additional http request
    private function slideshare_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => true,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function slideshare_req(){
        return "http://www.slideshare.net/api/oembed/2?format=json&amp;url=".$this->slideshare_url();
    }
    //return thumbnails
    private function slideshare_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["small"] = $res["thumbnail"]."-2";
            $arr["medium"] = $res["thumbnail"];
            $arr["large"] = $res["thumbnail"];
        }
        return $arr;
    }
    //return size
    private function slideshare_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function slideshare_iframe($res){   
        $code = explode("-", $this->code);
        $json = json_decode($res);
        return "http://www.slideshare.net/slideshow/embed_code/".$json->slideshow_id;
    }
    //return embed url
    private function slideshare_embed(){
        return "";
    }
    //return canonical url
    private function slideshare_url(){
        return "http://www.slideshare.net/".($this->code);
    }
    //return website url
    private function slideshare_site(){
        return "http://www.slideshare.net";
    }
    //return title
    private function slideshare_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * HULU FUNCTIONS
    **************************/
    //which data needs additional http request
    private function hulu_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => true,
            "iframe" => true,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function hulu_req(){
        return "http://www.hulu.com/api/oembed.json?url=".$this->hulu_url();
    }
    //return thumbnails
    private function hulu_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["large"] = $res["thumbnail_url"];
            $arr["medium"] = $res["thumbnail_url"];
            $arr["small"] = $res["thumbnail_url"];
        }
        return $arr;
    }
    //return size
    private function hulu_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function hulu_iframe($res){
        $url = "";
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $url = $res["embed_url"];
        }
        return $url;
    }
    //return embed url
    private function hulu_embed($res){
        return $this->viddler_iframe($res);
    }
    //return canonical url
    private function hulu_url(){
        return "http://www.hulu.com/watch/".($this->code);
    }
    //return website url
    private function hulu_site(){
        return "http://www.hulu.com";
    }
    //return title
    private function hulu_title($res){
        return $this->oembed_title($res);
    }



    /**************************
    * FLICKR FUNCTIONS
    **************************/
    //which data needs additional http request
    private function flickr_data(){
        return array(
            "thumb" => true, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function flickr_req(){
        return "http://www.flickr.com/services/oembed/?format=json&url=".$this->flickr_url();
    }
    //return thumbnails
    private function flickr_thumb($res){
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res))
        {
            $arr["large"] = str_replace(".jpg", "_b.jpg", $res["url"]);
            $arr["medium"] = $res["url"];
            $arr["small"] = str_replace(".jpg", "_m.jpg", $res["url"]);
        }
        return $arr;
    }
    //return size
    private function flickr_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function flickr_iframe(){
        return "";
    }
    //return embed url
    private function flickr_embed(){
        return "";
    }
    //return canonical url
    private function flickr_url(){
        return "http://www.flickr.com/photos/".($this->code);
    }
    //return website url
    private function flickr_site(){
        return "http://www.flickr.com";
    }
    //return title
    private function flickr_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * FUNNYORDIE FUNCTIONS
    **************************/
    private function funnyordie_decode(){
        $parts = explode("/", $this->code);
        return $parts[0];
    }
    //which data needs additional http request
    private function funnyordie_data(){
        return array(
            "thumb" => false, 
            "size" => true, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => true
        );
    }
    //return http request url where to get data
    private function funnyordie_req(){
        return "http://www.funnyordie.com/oembed?format=json&url=".$this->funnyordie_url();
    }
    //return thumbnails
    private function funnyordie_thumb(){
        $arr = array();
        $arr["large"] = "http://assets.ordienetworks.com/tmbs/".($this->funnyordie_decode())."/fullsize_11.jpg";
        $arr["medium"] = "http://assets.ordienetworks.com/tmbs/".($this->funnyordie_decode())."/large_11.jpg";
        $arr["small"] = "http://assets.ordienetworks.com/tmbs/".($this->funnyordie_decode())."/medium_11.jpg";
        return $arr;
    }
    //return size
    private function funnyordie_size($res){
        return $this->oembed_size($res);
    }
    //return iframe url
    private function funnyordie_iframe(){
        return "http://public0.ordienetworks.com/flash/fodplayer.swf?key=".$this->funnyordie_decode();
    }
    //return embed url
    private function funnyordie_embed(){
        return $this->funnyordie_iframe();
    }
    //return canonical url
    private function funnyordie_url(){
        return "http://www.funnyordie.com/videos/".($this->code);
    }
    //return website url
    private function funnyordie_site(){
        return "http://www.funnyordie.com";
    }
    //return title
    private function funnyordie_title($res){
        return $this->oembed_title($res);
    }

    /**************************
    * TWITPIC FUNCTIONS
    **************************/
    //which data needs additional http request
    private function twitpic_data(){
        return array(
            "thumb" => false, 
            "size" => false, 
            "embed" => false,
            "iframe" => false,
            "url" => false,
            "site" => false, 
            "title" => false
        );
    }
    //return http request url where to get data
    private function twitpic_req(){
        return "";
    }
    //return thumbnails
    private function twitpic_thumb(){
        $arr = array();
        $arr["large"] = "http://twitpic.com/show/full/".($this->code).".jpg";
        $arr["medium"] = "http://twitpic.com/show/large/".($this->code).".jpg";
        $arr["small"] = "http://twitpic.com/show/thumb/".($this->code).".jpg";
        return $arr;
    }
    //return size
    private function twitpic_size(){
        return "";
    }
    //return iframe url
    private function twitpic_iframe(){
        return "";
    }
    //return embed url
    private function twitpic_embed(){
        return "";
    }
    //return canonical url
    private function twitpic_url(){
        return "http://twitpic.com/".($this->code);
    }
    //return website url
    private function twitpic_site(){
        return "http://twitpic.com";
    }
    //return title
    private function twitpic_title(){
        return "";
    }


}
?>

I forgot to say that the urls are stored in the database and then returned with queries

Member Avatar for diafol

That's a lot of code for one class. Have you thought about subclassing an abstract parent / providing an interface?

Having subclasses with a contract to provide certain methods and properties should allow you to add new resources as and when you need. Your current "all in one" class makes maintenance difficult. Sorry that was a bit off-topic, but it was hard work rooting through 1000 lines of code - I shudder to think what you'd need to do if you wanted to edit/remove/add a resource.

commented: thanks diafol +2

Thanks Diafol. Can you give me an examble how to do that?

To my defence its a lot of classes in one file

Member Avatar for diafol

No defence required. Just a suggestion. Am busy right now. Hope to get back to you.

I don't mean this personally, but the class above will eat the server resources like a hog.

It is a class of too many methods. One probable solution I can think of is to break this class into multiple classes. There is a design pattern called Abstract Factory/Factory Method patterns which I think can work greatly on this type of application. Most importantly, if you are aiming for cache system.

The implementation of Abstract Factory/Factory Method patterns is kinda like this. ( Mr. LDA above thought me how to this, so any corrections are greatly appreciated). It appears to me that the class above is responsible for providing video contents coming from the external sites.

We can make a video factory class like this

class VideoFactory
{
    private $ytube_content;
    private $imgur_content;
    private $dmotion_content;
    private $flickr_content;

    public function __construct(YouTube $ytube_content, Imgur $imgur_content, DailyMotion $dmotion_content, Flickr $flickr_content)
    {
        $this->ytube_content = $ytube_content;
        $this->imgur_content = $imgur_content;
        $this->dmotion_content = $dmotion_content;
        $this->flickr_content = $flickr_content;

     }

     /*
     * get the youtube data
     */

     public function get_youtube()
     {
         return clone $this->ytube_content;
      }

      /*
      * get imgur data
      */
      public function get_imgur()
      {
          return clone $this->imgur_content;

       }

       /*
       * get dailymotion data
       */
       public function get_dailymotion()
       {
           return clone $this->dmotion_content;

        }

        /*
        * get flickr data
        */
        public function get_flickr()
        {
            return clone $this->flickr_content;

        }


} 

the video classes as broken down

class YouTube
{

    public function __construct()
    {
    }

    public function set_video()
    {

        //return all youtube video data

     }

     public function make_cache()
     {
         //you can make a cache file for this data

      }

   }

   class Imgur
   {
     // put the imgur get_data method
     public function imgur_data()
     {
     }
     // rest of the methods related to imgur method
   }

   class DailyMotion
   {

   }

   class Flickr
   {

   }

We can conservatively run our factory like this

$flixFactory = new VideoFactory(new Youtube(), new Imgur(), new DailyMotion(), new Flickr());

we can test our video factory like this

print_r($flixFactory->get_youtube());

that should return us YouTube object

We can capture all youtube content via factory like this

$Youtube_video_array = $flixFactory->get_youtube()->set_video();

in my example above, the $youtube_video_array can be iterated and the resulting items can be cached any way you want it.

If you have methods that are called inside of more than one methods, then you can group these methods into one parent class and then the video classes will have to extend the newly created class.

for example, if the method responsible for acquiring thumb urls, embed codes, comments, etc, we can create a parent class like this

class MainClass
{
    public function __construct()
    {

    }

    public function get_thumb($source = null, $url=null)
    {
        return $thumb_url;

    }

    public function get_embed_code( $source = null, $url=null)
    {

        return $embed_code;

    }

}

The video classes should now be able to extend the main class

 class YouTube extends MainClass
 {
     public function __construct()
     {
     parent::__consruct();
     }

     public function set_video()
     {
         return array(parent::get_thumb('Youtube'),'');
     }

 }

there are so many things you can do with object oriented programming to save server resources.

commented: thanks veedeoo +2

Thanks Veedeoo i have a couple of questions...

So all i have to do is to put this

function __construct($input){
        foreach($this->all_types as $site => $types)
        {
            foreach($types as $type => $regexp)
            {
                preg_match($regexp, $input, $match);
                if(!empty($match))
                {
                    /*echo "<p>".$site." ".$type."</p>";
                    echo "<pre>";
                    print_r($match);
                    echo "</pre>";*/
                    for($i = 1; $i < sizeof($match); $i++)
                    {
                        if($match[$i] != "")
                        {
                            $this->code = $match[$i];
                            $this->site = $site;
                            break;
                        }
                    }
                    if($this->code != "")
                    {
                        break;
                    }
                }
            }
            if($this->code != "")
            {
                break;
            }
        }
    }

here

class YouTube
{
    public function __construct()
    {
    here
    }
}

But then again i put it here?

class MainClass
{
    public function __construct()
    {

    }

And call again it with this?

class YouTube extends MainClass
{
    public function __construct()
    {
        parent::__consruct();
    }

This is for examble for youtube. I ll do the same thing for Dailymotion, Vimeo, and so on right?
But this way i ll be repeating the same code for its case (youtube, dailymotion, vimeo etc).

A second thing is that i dont know if the url the user is uploading is from youtube or vimeo or dailymotion and so on. The url is inserted on a <input text>. So i cant use the line below right?
$Youtube_video_array = $flixFactory->get_youtube()->set_video();

Any help is truely needed...

Let me work on it.. I'll post it tomorrow evening my time zone western pacific.

ok, after looking at your code, I decided to tweak my recommendations above.

Before anything else, we need to do some refactoring on your codes. All of the methods responsible for video data shares the same properties and variables.

for example let us take a look at the simple table below

+------------+-------+------+-------+--------+-------+-------+-------+
+ Provider   + thumb + size + embed + iframe +  url  + site  + title +
+------------+-------+------+-------+--------+-------+-------+-------+
+ youtube    + false + true + false + false  + false + false + true  +
+------------+-------+------+-------+--------+-------+-------+-------+
+ imgur      + true  + true + false + false  + false + false + true  +
+------------+-------+------+-------+--------+-------+-------+-------+
+ deviantart + true  + true + false + false  + false + false + true  +
+------------+-------+------+-------+--------+-------+-------+-------+ 
+ soundcloud + true  + true + false + true   + true  + false + true  +
+------------+------------+---------+--------+-------+-------+-------+

With simple analysis of the table above, we can easily draw a simple conclusion that the nearest values of the columns where the video providers can have very minimal differences is when we define the default values as shown on the table below..

+------------+-------+------+-------+--------+-------+-------+-------+
+ Provider   + thumb + size + embed + iframe +  url  + site  + title +
+------------+-------+------+-------+--------+-------+-------+-------+
+ default    + true  + true + false + false  + false + false + true  +
+------------+-------+------+-------+--------+-------+-------+-------+

All video providers are also utilizing either oembed_size() and oembed_title() OR og_size() and og_title methods. All video providers also uses these variables $code and $res.

Those are the most important information to refactor our class..

Refactoring

We create a VideoData class with static function get_video_data() method or whatever name you are comfortable with. I can't think of any right now.

here is the bare class

class VideoData
{

public function __construct()
{

}

public static function get_video_data($source, $code = null, $res = null)
{
    /*
     * define default values
     */
     $thumb = true;
     $size = true;
     $embed = false;
     $iframe = false;
     $url = false;
     $site = false;
     $title = true;

     switch($source){

            case "youtube":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
            $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }

            $thumb = false;

            $url_req = "http://www.youtube.com/watch?v=".($code);
            $v_thumb = $arr;
            $iframe_url = "http://www.youtube.com/embed/".($code);
            $embed_url = "http://www.youtube.com/v/".($code);
            $o_size = self::og_size($res);
            $con_url = "http://www.youtube.com/watch?v=".($code);
            $w_url = "http://www.youtube.com";
            $i_title = self::og_title($res);
        break;

        /*
        * add more  provider below
        */

        } //end of switch

        return array(
                      'v_data' => array(
                                        "thumb" => $thumb,
                                        "size" => $size,
                                        "embed" => $embed,
                                        "iframe" => $iframe,
                                        "url" => $url,
                                        "site" => $site,
                                        "title" => $title
                                        ),
                        'url_reg' => $url_req,
                        'v_thumb' => $v_thumb,
                        'iframe_url' => $iframe_url,
                        'embed_url' => $embed_url,
                        'o_size' => $o_size,
                        'con_url' => $con_url,
                        'w_url' => $w_url,
                        'i_title' => $i_title
                        );

        } // end of method
  }

The idea of having the get_video_data as static is to avoid the object creation. If you want to set it public it is fine also, but you need to instantiate the class.

to use the above class, just include the file

require_once('videodata.php');

$youtube = VideoData::get_video_data('youtube', $code = null, $res = null);

$dailymotion = VideoData::get_video_data('dailymotion', $code = null, $res = null);

remember you will have to provide the $code and $res from the original class you have posted.

Remove all of the provider functions from the original class.

If you want to add constructor and deconstructor to the videoData class, you can do so. However, if you want to set get_video_data() method to be public, then the objects must be created like this

$videoObject = new VideoData();

$youtube = $videoObject->get_video_data('youtube', $code = null, $res = null);

The output of the script is that can be cached is the array from the get_video_data() method.

$video_data = $youtube[1];

should give you the array about the video from youtube e.g. title, thumb, url and others.

If you know how to use Smarty Template engine, you can also use the cache option to on.

Here is link to a sample file for your reference. Please do not hesitate to come back and post any concerns you may have..

Update: ON the sample file, remove the extra curly bracket line 222...

 } // end of method

and put this on line 192..

} //end of switch

to help you out guys, here is the complete refactoring reference table for all video getters.

                +--------+
                + thumb  + 
+---------------+--------+
+ youtube       + false  +
+---------------+--------+
+ funnyordie    + false  + 
+---------------+--------+
+ dotsub        + false  +
+---------------+--------+

           +-------+-------+-------+
           + thumb + size  + url   +
+----------+-------+-------+-------+
+ metacafe + false + false + false +
+----------+-------+-------+-------+
+ twitpic  + false + false + false +
+----------+-------+-------+-------+

       +-------+-------+--------+
       + size  + embed + iframe +
+------+-------+-------+--------+
+ blip + false + true  + true   +
+------+-------+-------+--------+

            +-------+--------+
            + embed + iframe +
+-----------+-------+--------+
+ revision  + true  + true   +
+-----------+-------+--------+
+ videojug  + true  + true   +
+-----------+-------+--------+
+ hulu      + true  + true   +
+-----------+-------+--------+


             +--------+
             + iframe +
+------------+--------+
+ slideshare + true   + 
+------------+--------+

                +--------+-------+
                + iframe + url   +
+---------------+--------+-------+
+ soundcloud    +  true  + true  +
+---------------+--------+-------+
+ kickstarter   +  true  + true  +
+---------------+--------+-------+
+ ted           +  true  + true  +
+---------------+--------+-------+

                +--------+
                + url    + 
+---------------+--------+
+ instagram     +  true  +
+---------------+--------+

by Veedeoo's recommendation the tables above are the refactored values against the defined default value..

Also, I would like to agree on the smarty template engine as viable option in creating cached content. At least, if you choose this option, you will have to do it once. If the source code will be needing refactoring in the future, the template will only need minor modification as needed.

Thanks Veedeoo and LDA. Its kind difficult for me to figure out so it will take at least a day to write it down for all external servers. I ll post what i ll write here and if i have any questions i ll ask you.

Let me post the output -how i call the videos and photos from these providers.

<?php
if(textlink($orimessage))
{
$link =textlink($orimessage);
$em = new Wall_Expand($link);
$site = $em->get_site();
if($site != "")
{
$code = $em->get_iframe();
if($code == "")
{
$code = $em->get_embed();
if($code == "")
{
$codesrc=$em->get_thumb("large");
}
}
if($codesrc)
{
echo '<div class="img_container"><img src='.$codesrc.' class="imgpreview" /></div>';
}
else if($code)
echo $code;
}
} 
$codesrc='';
$code='';
?>

orimessage is the url that the user inserts in the input

I understand how this work. You set the default first and then you set the parameters for each provider. But how do i set the width and the height? I guess the classes you created are for not repeating the same functions for each provider. Right? Below i am posting the whole file as i understand it. What about in lines 187, 215, 234, 253, 275, 294, 314, 335, 357, 379, 397, 416, 436, 454, 473. $arr What is this array?

<?php

class VideoData
{
    private $default_size = array("w" => 472, "h" => 268);
    private $default_size_tiny = array("w" => 230, "h" => 170);
    public function __construct($input)
    {
        foreach($this->all_types as $site => $types)
        {
            foreach($types as $type => $regexp)
            {
                preg_match($regexp, $input, $match);
                if(!empty($match))
                {
                    /*echo "<p>".$site." ".$type."</p>";
                    echo "<pre>";
                    print_r($match);
                    echo "</pre>";*/
                    for($i = 1; $i < sizeof($match); $i++)
                    {
                        if($match[$i] != "")
                        {
                            $this->code = $match[$i];
                            $this->site = $site;
                            break;
                        }
                    }
                    if($this->code != "")
                    {
                        break;
                    }
                }
            }
            if($this->code != "")
            {
                break;
            }
        }
    }

    public static function get_video_data($source, $code = null, $res = null)
    {
        /*
         * define default values
         */
         $thumb = true;
         $size = true;
         $embed = false;
         $iframe = false;
         $url = false;
         $site = false;
         $title = true;


        switch($source)
        {
            case "youtube":
            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }
            $thumb = false;

            $url_req = "http://www.youtube.com/watch?v=".($code);
            $v_thumb = $arr;
            $iframe_url = "http://www.youtube.com/embed/".($code);
            $embed_url = "http://www.youtube.com/v/".($code);
            $o_size = self::og_size($res);
            $con_url = "http://www.youtube.com/watch?v=".($code);
            $w_url = "http://www.youtube.com";
            $i_title = self::og_title($res);
            break;

            case "imgur":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $url_req = "http://api.imgur.com/oembed/?url=http://imgur.com/gallery/".($code);
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.imgur.com";
            $i_title = self::oembed_title($res);
            break;

            case "deviantart":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $url_req = "http://backend.deviantart.com/oembed?format=json&url=http://deviantart.com/". ($code); 
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.deviantart.com"; 
            $i_title = self::oembed_title($res);    
            break;

            case "soundcloud":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {       
                $arr['medium'] = $res['thumbnail_url'];
            }

            $data = json_decode($res);
            $if_array = array();
            preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;
            $iframe = true;
            $url = true;
            $url_req = "http://soundcloud.com/oembed?format=json&url=http://soundcloud.com/". ($code);
            $v_thumb = $arr;
            $iframe_url = $if_array[1];
            $embed_url = "";
            $o_size = array("w" => "100%", "h" => 166);
            $con_url = "http://soundcloud.com/". ($code);
            $w_url = "http://www.soundcloud.com";
            $i_title = self::oembed_title($res);
            break;

            case "kickstarter":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {       
                $arr['medium'] = $res['thumbnail_url'];
            }

            $data = json_decode($res);
            $if_array = array();
            preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;
            $iframe = true;
            $url = true;
            $url_req = "http://www.kickstarter.com/services/oembed?url=".urlencode('http://www.kickstarter.com/').($code);
            $v_thumb = $arr;
            $iframe_url = $if_array[1];
            $embed_url = "";
            $o_size = array("w" => "100%");
            $con_url = "http://www.kickstarter.com/".($code);
            $w_url = "http://www.kickstarter.com";
            $i_title = self::oembed_title($res);
            break;

            case "instagram":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                //echo $res = current($res); exit;
                $arr['medium'] = $res['url'];
            }
            $url = true;
            $url_req = "http://api.instagram.com/oembed?url=http://instagram.com/".($code);
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "http://instagram.com/".($code);
            $w_url = "http://www.instagram.com";
            $i_title = self::oembed_title($res);
            break;

            case "ted":

            $res = json_decode($res, true);
                $arr = array();
                if(is_array($res) && !empty($res)){

                $arr['medium'] = $res['thumbnail_url'];

                }

                $data = json_decode($res);
                $if_array = array();
                preg_match( '/src="([^"]*)"/i', $data->html, $array ) ;


                $iframe = true;
                $url = true;

                $url_req = "http://www.ted.com/talks/oembed?format=json&url=http://www.ted.com/". ($code);
                $v_thumb = $arr;
                $iframe_url = $if_array[1];
                $embed_url = "";
                $o_size = array("w" => "100%", "h" => 166);
                $con_url = "http://www.ted.com/". ($code);
                $w_url = "http://www.ted.com";
                $i_title = self::oembed_title($res);
            break;

            case "vimeo":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }

            $url_req = "http://www.vimeo.com/".($code);
            $v_thumb = $arr;
            $iframe_url = "http://player.vimeo.com/video/".($code);
            $embed_url = "http://vimeo.com/moogaloop.swf?clip_id=".($code);
            $o_size = self::og_size($res);
            $con_url = "http://www.vimeo.com/".($code);
            $w_url = "http://www.vimeo.com";
            $i_title = self::og_title($res);
            break;

            case "dailymotion":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }

            $url_req = "http://www.dailymotion.com/".($code);
            $v_thumb = $arr;
            $iframe_url = "http://www.dailymotion.com/embed/video/".($code);
            $embed_url = "http://www.dailymotion.com/swf/video/".($code);
            $o_size = self::og_size($res);
            $con_url = "http://www.dailymotion.com/".($code);
            $w_url = "http://www.dailymotion.com";
            $i_title = self::og_title($res);
            break;

            case "metacafe":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }
            $thumb = false;
            $url = false;
            $size= false;

            $url_req = "http://www.metacafe.com/watch/".($code);
            $v_thumb = $arr;
            $iframe_url = "http://www.metacafe.com/fplayer/".$code.".swf";
            $embed_url = "http://www.metacafe.com/fplayer/".$code.".swf";
            $o_size = self::og_size($res);
            $con_url = "http://www.metacafe.com/watch/".($code);
            $w_url = "http://www.metacafe.com";
            $i_title = self::og_title($res);
            break;

            case "clikthrough":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }

            $url_req = "http://www.clikthrough.com/services/oembed/?url=".$code."%26format%3Djson";
            $v_thumb = $arr;
            $iframe_url = "http://www.clikthrough.com/clikPlayer.swf?videoId=".$code;
            $embed_url = "http://www.clikthrough.com/clikPlayer.swf?videoId=".$code;
            $o_size = self::og_size($res);
            $con_url = "http://www.clikthrough.com/services/oembed/?url=".$code."%26format%3Djson";
            $w_url = "http://www.clikthrough.com";
            $i_title = self::og_title($res);
            break;

            case "dotsub":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }
            $thumb = false;

            $url_req = "http://dotsub.com/services/oembed?url=".$code;
            $v_thumb = $arr;
            $iframe_url = "http://dotsub.com/static/players/portalplayer.swf?uuid=".$code."&lang=eng&plugins=dotsub&embedded=true";
            $embed_url = "http://dotsub.com/static/players/portalplayer.swf?uuid=".$code."&lang=eng&plugins=dotsub&embedded=true";
            $o_size = self::og_size($res);
            $con_url = "http://dotsub.com/services/oembed?url=".$code;
            $w_url = "http://www.dotsub.com";
            $i_title = self::og_title($res);
            break;

            case "videojug":

            $size_types = array("small" => "default", "medium" => "hqdefault", "large" => "hqdefault");
            $arr = array();
            foreach($size_types as $key => $val)
            {
                $arr[$key] = "http://i.ytimg.com/vi/". ($code) ."/". $val .".jpg";
            }
            $iframe = true;
            $embed= true;

            $url_req = "http://www.videojug.com/film/".$code;
            $v_thumb = $arr;
            $iframe_url = "http://dotsub.com/static/players/portalplayer.swf?uuid=".$code."&lang=eng&plugins=dotsub&embedded=true";
            $embed_url = "http://dotsub.com/static/players/portalplayer.swf?uuid=".$code."&lang=eng&plugins=dotsub&embedded=true";
            $o_size = self::og_size($res);
            $con_url = "http://www.videojug.com/film/".$code;
            $w_url = "http://www.videojug.com";
            $i_title = self::og_title($res);
            break;

            case "revision":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }

            $iframe = true;
            $embed= true;

            $url_req = "http://revision3.com/".($code);
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://revision3.com";
            $i_title = self::oembed_title($res);
            break;

            case "blip":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $size = false;
            $embed = true;
            $iframe = true;

            $url_req = "http://blip.tv/".($code);
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://blip.tv";
            $i_title = self::oembed_title($res);
            break;

            case "screenr":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $url_req = "http://www.screenr.com/api/oembed.json?url=".$code;
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.screenr.com";
            $i_title = self::oembed_title($res);
            break;

            case "slideshare":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $iframe = true;
            $url_req = "http://www.slideshare.net/api/oembed/2?format=json&amp;url=".$code;
            $v_thumb = $arr;
            $iframe_url = "http://www.slideshare.net/slideshow/embed_code/".$code;
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.slideshare.net";
            $i_title = self::oembed_title($res);
            break;

            case "hulu":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $iframe = true;
            $embed= true;
            $url_req = "http://www.hulu.com/api/oembed.json?url=".$code;
            $v_thumb = $arr;
            $iframe_url = "http://www.slideshare.net/slideshow/embed_code/".$code;
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.hulu.com";
            $i_title = self::oembed_title($res);
            break;

            case "flickr":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $url_req = "http://www.flickr.com/services/oembed/?format=json&url=".$code;
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.flickr.com";
            $i_title = self::oembed_title($res);
            break;

            case "funnyordie":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $thumb = false;
            $url_req = "http://www.funnyordie.com/oembed?format=json&url=".$code;
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.funnyordie.com";
            $i_title = self::oembed_title($res);
            break;

            case "twitpic":

            $res = json_decode($res, true);
            $arr = array();
            if(is_array($res) && !empty($res))
            {
                $arr['medium'] = $res['url'];
            }
            $thumb = false;
            $url = false;
            $size= false;
            $url_req = "http://twitpic.com/".$code;
            $v_thumb = $arr;
            $iframe_url = "";
            $embed_url = "";
            $o_size = self::oembed_size($res);
            $con_url = "";
            $w_url = "http://www.twitpic.com";
            $i_title = self::oembed_title($res);
            break;
        } //end of switch


        return array(
        'v_data' => array(
                        "thumb" => $thumb,
                        "size" => $size,
                        "embed" => $embed,
                        "iframe" => $iframe,
                        "url" => $url,
                        "site" => $site,
                        "title" => $title
                        ),
        'url_reg' => $url_req,
        'v_thumb' => $v_thumb,
        'iframe_url' => $iframe_url,
        'embed_url' => $embed_url,
        'o_size' => $o_size,
        'con_url' => $con_url,
        'w_url' => $w_url,
        'i_title' => $i_title
        );
    }

    /**************************
    * PUBLIC FUNCTIONS
    **************************/

    public function get_thumb($size = "small"){
        if($this->site != "")
        {
            $size_types = array("small", "medium", "large");
            $size = strtolower($size);
            if(!in_array($size, $size_types))
            {
                $size = "small";
            }
            $this->prepare_data("thumb");
            return $this->data[$size];
        }
        else
        {
            return "";
        }
    }

    public function get_iframe($w = -1, $h = -1){
        $this->prepare_data("iframe");
        if($this->site != "" && $this->data["iframe"] != "")
        {

            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size["h"];
            }
            $w=470;
            return '<iframe class="new" width="'.$w.'" height="'.$h.'" src="'.$this->data["iframe"].'" frameborder="0" allowfullscreen></iframe>';
        }
        else
        {
            return "";
        }
    }

    public function get_iframe_tiny($w = -1, $h = -1){
        $this->prepare_data("iframe");
        if($this->site != "" && $this->data["iframe"] != "")
        {

            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size_tiny["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size_tiny["h"];
            }
            $w=230;
            return '<iframe class="new" width="'.$w.'" height="'.$h.'" src="'.$this->data["iframe"].'?autoplay=1" frameborder="0" allowfullscreen></iframe>';
        }
        else
        {
            return "";
        }
    }

    public function get_embed($w = -1, $h = -1){
        $this->prepare_data("embed");
        if($this->site != "" && $this->data["embed"])
        {
            if($w < 0 || $h < 0)
            {
                $w = (is_int($this->data["w"]) && $this->data["w"] > 0) ? $this->data["w"] : $this->default_size["w"];
                $h = (is_int($this->data["h"]) && $this->data["h"] > 0) ? $this->data["h"] : $this->default_size["h"];
            }
            return '<object width="'.$w.'" height="'.$h.'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="'.$this->data["embed"].'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'.$this->data["embed"].'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$w.'" height="'.$h.'"></embed></object>';
        }
        else
        {
            return "";
        }
    }

    public function get_url(){
        if($this->site != "")
        {
            $this->prepare_data("url");
            return $this->data["url"];
        }
        else
        {
            return "";
        }
    }

    public function get_id(){
        return $this->code;
    }

    public function get_site(){
        $this->prepare_data("site");
        return $this->data["site"];
    }

    public function get_size(){
        $arr = array();
        $this->prepare_data("size");
        $arr["w"] = ($this->data["w"] < 0) ? $this->default_size["w"] : $this->data["w"];
        $arr["h"] = ($this->data["h"] < 0) ? $this->default_size["h"] : $this->data["h"];
        return $arr;
    }

    public function get_title(){
        $this->prepare_data("title");
        return $this->data["title"];
    }

    /**************************
    * PRIVATE FUNCTIONS
    **************************/
    private function get_data($url){
        //echo "<p>Curl request ".$url."</p>";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
        $curlData = curl_exec($curl);
        curl_close($curl);
        return $curlData;
    }

    private function prepare_data($type){
        if($this->site != "")
        {
            $ready = false;
            switch($type)
            {
                case "size":
                    if($this->data["w"] > 0 && $this->data["h"] > 0)
                    {
                        $ready = true;
                    }
                break;
                case "thumb":
                    if($this->data["small"] != "" && $this->data["medium"] != "" && $this->data["large"] != "")
                    {
                        $ready = true;
                    }
                break;
                default:
                if($this->data[$type] != "")
                {
                    $ready = true;
                }
            }
            //if information is not yet loaded
            if(!$ready)
            {
                $func = ($this->site)."_data";
                $arr = $this->$func();
                //check if information requires http request
                if(!$arr[$type])
                {
                    //if not, just provide data
                    $func = ($this->site)."_".$type;
                    $this->aggregate($this->$func(), $type);
                }
                else
                {
                    //else if it needs http request we may as well load all other data
                    //so we won't need to request it again
                    $req = ($this->site)."_req";
                    $res = $this->get_data($this->$req());
                    foreach($arr as $key => $val)
                    {
                        $func = ($this->site)."_".$key;
                        if($val)
                        {
                            $this->aggregate($this->$func($res), $key);
                        }
                        else
                        {
                            $this->aggregate($this->$func(), $key);
                        }
                    }
                }
            }
        }
    }

    private function aggregate($data, $type){
        if(is_array($data))
        {
            foreach($data as $key => $val)
            {
                $this->data[$key] = $val;
            }
        }
        else
        {
            $this->data[$type] = $data;
        }
    }


    public function og_size($res)
    {
        $arr = array();
        preg_match( '/property="og:video:width"\s*content="([\d]*)/i', $res, $match);

        if(!empty($match))
        {
            $arr["w"] = (int)$match[1];
        }

        preg_match( '/property="og:video:height"\s*content="([\d]*)/i', $res, $match);
        if(!empty($match))
        {
            $arr["h"] = (int)$match[1];
        }
        return $arr;
    }

    public function og_title($res)
    {
        $ret = "";
        preg_match( '/property="og:title"\s*content="([^"]*)"/i', $res, $match);

        if(!empty($match))
        {
            $ret = $match[1];
        }
        return $ret;
    }

    //oembed functions
    public function oembed_size($res)
    {
        $arr = array();
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res) && isset($res["width"]) && isset($res["height"]))
        {
            $arr["w"] = (int)$res["width"];
            $arr["h"] = (int)$res["height"];
        }
        return $arr;
    }

    public function oembed_title($res)
    {
        $title = "";
        $res = json_decode($res, true);
        if(is_array($res) && !empty($res) && isset($res["title"]))
        {
            $title = $res["title"];
        }
        return $title;
    }
}

Do i need to ask you or to post something else?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.