我正在尝试使用以下代码嵌入Facebook视频:
<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>
它工作正常,但有没有类似的方式来显示视频ID的视频缩略图?
例如:http://www.facebook.com/thumbnail/115316011865684
或其他什么?
您可以转到此图谱API网址 - https://graph.facebook.com/VIDEO_ID/picture,例如,从视频ID中获取缩略图。 https://graph.facebook.com/115316011865684/picture
https://graph.facebook.com/VIDEO_ID将为您提供更多信息,包括更大的缩略图。 (您可以在https://developers.facebook.com/docs/graph-api/reference/video获取可用信息的列表。)
这里有一些PHP代码来挖掘最大的缩略图:
$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;
}
更新:上面的代码已经更新,因为Facebook在2017年7月10日更改了他们的API。以下是一些额外的PHP代码,以便在Facebook再次更改内容时获取视频的大缩略图:
$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;
}
第二种解决方案需要Facebook访问令牌。以下是有关如何获取Facebook访问令牌的一些说明:https://smashballoon.com/custom-facebook-feed/access-token/
我得到它:
https://graph.facebook.com/VIDEO_ID?fields=format,source
这将为您提供一系列可用格式,包含用于嵌入的缩略图URL和HTML。此外,源属性获取视频的.mp4网址。
尝试:https://graph.facebook.com/1706818892661729?fields=format,source
我创建了一个PHP函数来回答你的问题,而你不必阅读有关facebook图的无聊文档。您只需要插入视频链接,Facebook和YouTube,但您可以修改以添加其他来源。只需复制地址栏中的youtube视频链接,对于Facebook,右键单击视频,然后单击显示视频网址,然后复制即可。
//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;
}