Jump to content

Is this the best way to generate a Youtube/Vimeo/FB video embed code?


imgrooot

Recommended Posts

I am looking for a code where I can get a video URL from Youtube, Vimeo, Facebook and convert it to an embedded video to be shown on my site. I found a script that does that. It works fine.

The only errors I get are in the inspect element window in the browser. Errors such as this. 

Failed to load resource: net::ERR_BLOCKED_BY_CLIENT   googleads.g.doubleclick.net/pagead/id:1 
Failed to load resource: net::ERR_BLOCKED_BY_CLIENT   static.doubleclick.net/instream/ad_status.js:1 

 

This is the function to generate the embedded videos. Do the above errors matter if the videos show up and play fine?

function generateVideoEmbedUrl($url){
    //This is a general function for generating an embed link of an FB/Vimeo/Youtube Video.
    $finalUrl = '';
    if(strpos($url, 'facebook.com/') !== false) {
        //it is FB video
        $finalUrl.='https://www.facebook.com/plugins/video.php?href='.rawurlencode($url).'&show_text=1&width=200';
    }else if(strpos($url, 'vimeo.com/') !== false) {
        //it is Vimeo video
        $videoId = explode("vimeo.com/",$url)[1];
        if(strpos($videoId, '&') !== false){
            $videoId = explode("&",$videoId)[0];
        }
        $finalUrl.='https://player.vimeo.com/video/'.$videoId;
    }else if(strpos($url, 'youtube.com/') !== false) {
        //it is Youtube video
        $videoId = explode("v=",$url)[1];
        if(strpos($videoId, '&') !== false){
            $videoId = explode("&",$videoId)[0];
        }
        $finalUrl.='https://www.youtube.com/embed/'.$videoId;
    }else if(strpos($url, 'youtu.be/') !== false){
        //it is Youtube video
        $videoId = explode("youtu.be/",$url)[1];
        if(strpos($videoId, '&') !== false){
            $videoId = explode("&",$videoId)[0];
        }
        $finalUrl.='https://www.youtube.com/embed/'.$videoId;
    }else{
        //Enter valid video URL
    }
    return $finalUrl;
}

 

Link to comment
Share on other sites

47 minutes ago, requinix said:

Do you know what those two errors even mean? Do you know what those domains are? Do you see any particular words in those URLs that might give you a clue?

I just looked those errors up and they have something to do with AdBlocker extension I have installed on the browser. 

I disabled the AdBlocker extension on that page and reloaded it. Now all the errors are gone. Which is great news.

 

Edited by imgrooot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.