开发者

How to get Facebook video thumbnail from its video id?

I am trying to embed Facebook video using the code below:

<object width="400" height="224" >
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http:开发者_运维知识库//www.facebook.com/v/115316011865684" />
<embed src="http://www.facebook.com/v/115316011865684" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="224">
</embed>
</object>

It's working fine, but is there any similar way to show video thumbnail from video id?

For example: http://www.facebook.com/thumbnail/115316011865684 or something else?


You can get the thumbnail picture from the video id by going to this Graph API URL - https://graph.facebook.com/VIDEO_ID/picture, e.g. https://graph.facebook.com/115316011865684/picture


https://graph.facebook.com/VIDEO_ID will give you a lot more info, including bigger thumbnails to pick from. (You can get a list of the available info at https://developers.facebook.com/docs/graph-api/reference/video.)

Here's some PHP code to dig up the biggest thumbnail:

$data = file_get_contents("https://graph.facebook.com/$video_id?fields=format");
if ($data !== FALSE)
{
 $result=json_decode($data);
 $count=count($result->format)-1;
 $thumbnail=$result->format[$count]->picture;
}

Update: The code above has been updated since Facebook changed their API on July 10, 2017. Here is some additional PHP code to get a big thumbnail for a video in case Facebook changes things again:

$data = file_get_contents("https://graph.facebook.com/$video_id/thumbnails?access_token=$facebook_access_token");
if ($data !== FALSE)
{
 $result=json_decode($data);
 $thumbnail=$result->data[0]->uri;
}

This second solution requires a Facebook access token. Here are some instructions on how to get a Facebook access token: https://smashballoon.com/custom-facebook-feed/access-token/

Update: Facebook is making it more and more difficult to even get access tokens with the necessary permissions for such a simple task. Here is how to get the info from the raw HTML:

$data = `curl -s -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' -L 'https://www.facebook.com/1706818892661729/'`;

if (preg_match('#<video [^>]+></video>\s*<div [^>]+><img [^>]+src="([^"]+)#s',$data,$matches))
 {
  $image = $matches[1]; $image = str_replace('&amp;','&',$image);
  if (strpos($image,'&')) {print "Answer: $image\n";}
 }

Note that if you download the page, Facebook also provides the meta property twitter:image but that image is only 200x200. If Facebook wasn't such a pain in the butt, they would also provide the meta property og:image with a decent size image but they don't.


I just get it:

https://graph.facebook.com/VIDEO_ID?fields=format,source

This will get you an array of available formats with thumbnail URL and HTML for embeding. Also source attribute get .mp4 url of video.

Try: https://graph.facebook.com/1706818892661729?fields=format,source


i created a php function to answer your question without you having to go through reading the boring documentation about facebook graph. All you will need is just to insert your video link,facebook and youtube, but you can modify to add other sources. simply copy the youtube video link in the addres bar and for for facebook, right click on the video and click on show video url, then copy that.

    //get video thumbnail for facebook and youtube
function get_vid_thumbnail($link){
    $thumbnail='';
//check if video link is facebook
    if (strpos($link, 'facebook') !== false) {
        $thumbnail=fb_thumb($link);
        //$thumbnail='fb';
    }
//check if video link is youtube
    if (strpos($link, 'youtube.com') !== false) {
        $thumbnail=youtube_thumb($link);
        //$thumbnail='youtube';
    }
    return $thumbnail;
}


//supporting functions
//get youtube thumbnail
function youtube_thumb($link){
    $new=str_replace('https://www.youtube.com/watch?v=','', $link);
    $vv='https://img.youtube.com/vi/'.$new.'/0.jpg';
    return $vv;
}

//clean the facebook link
function fb_video_id($url) {
    //split the url
    $main=parse_url($url);
    //get the pathe and split to get the video id
    $main=$main['path'];
    $main=explode('/',$main);
    $main=$main[3];
    return $main;
}
//get the thumbnail
function fb_thumb($link) {
    $img = 'https://graph.facebook.com/'.fb_video_id($link).'/picture';
    return $img;
}

//get video thumbnail for fb and youtube ends

//get embed url for facebook and youtube to be used as video source
function get_vid_embed_url($link){
    $embed_url='sss';
//check if video link is facebook
    if (strpos($link, 'facebook') !== false) {
        # code...
        $embed_url=fb_embed_link($link);
        //$thumbnail='fb';
    }
//check if video link is youtube
    if (strpos($link, 'youtube.com') !== false) {
        # code...
        $embed_url=youtube_embed_link($link);
        //$thumbnail='youtube';
    }
    return $embed_url;
}
//get youtube embed link
function youtube_embed_link($link){
    $new=str_replace('https://www.youtube.com/watch?v=','', $link);
    $link='https://www.youtube.com/embed/'.$new;
    return $link;
}
//get facebook embed link
function fb_embed_link($link) {
    $link = 'https://www.facebook.com/plugins/video.php?href='.$link.'&show_text=0&width=560';
    return $link;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜