如何在 Facebook iFrame 选项卡上自定义每个视频的共享信息?

发布于 2024-12-21 17:47:01 字数 6938 浏览 0 评论 0原文

我有一个 facebook iframe 选项卡,上面有一些 youtube 视频,这些视频是直接从 youtube api 提供的 json 响应中提取的。

当您选择缩略图时,视频将嵌入选项卡顶部,然后可以播放。

嵌入视频下方还显示标题、描述、视图和共享按钮。

目前,每个视频的共享信息都是相同的,我正在绞尽脑汁如何为每个视频定制它,即使这意味着进行 ajax 调用。

这是相关代码:

$(document).ready(function() {

$('.share_button').live('click', function(e){
    // fade out video to stop it conflicting with dialogue box
$('#player').fadeOut('slow');
    e.preventDefault();
    FB.ui(
    {
        method: 'feed',
        name: 'generic name',
        link: 'generic link',
        picture: 'generic image',
        caption: 'generic caption',
        description: 'generic description'
    },
    function(response) {
        // fade video back in once the dialogue box is closed
        $('#player').show('slow');     
    });
});

我的选项卡的完整代码是:

$url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);

?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId : 'XXXXXXXXX',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml : true // parse XFBML
    });
};

(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>


<div id="placeholder" style="font-family:Helvetica; max-width:500px; margin:0 0 20px 0;">
<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][1]['id']; ?>" frameborder="0" allowfullscreen wmode="Opaque"></iframe></div>
<div id="title" style=" font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $data['data']['items'][1]['title']; ?> </div>
<div id="views" style="color:#BBB; font-size:12px;"><?php echo $data['data']['items'][1]['viewCount'];?> views</div>
<div id="description" style="font-size:14px;"><?php echo $data['data']['items'][1]['description'];?></div>
<img src = "../images/share_button.png" class="share_button" id="1" style="cursor:pointer;">
<div id="hr" style="padding:5px 0 10px 0; float:left; border-bottom: 1px solid #96CC90; width:480px; margin:0 0 15px 0;"></div>
</div>

<?php
$i = 0;

foreach($data['data']['items'] as $item) {
$id = $item['id'];
$thumb = $item['thumbnail']['sqDefault'];
?>
<div class="video" id="<?php echo $i; ?>" style="font-family:Helvetica; float:left; max-width:150px; min-height:130px; margin:0 0px 20px 10px; cursor:pointer;">
    <div class="thumb" style="position:relative; left:0; top:0;">
        <img src="<?php echo $item['thumbnail']['sqDefault'];?>" title="<?php echo $description;?>" style="position:relative; top:0; left:0px;"/>
        <img src="../images/play-thumb.png" style="position:absolute; top:30px; left:45px;"/>
    </div>
    <div class="title" style="font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $item['title']; ?></div>
    <div class="views" style="font-size:12px; cursor:text;"><?php echo $item['viewCount'];?> views</div>
</div>

<?php
$i++;
}
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$('.share_button').live('click', function(e){
    $('#player').fadeOut('slow');
    e.preventDefault();
    FB.ui(
    {
        method: 'feed',
        name: 'Generic Name',
        link: 'Generic Link',
        picture: 'Generic Picture',
        caption: 'Generic Caption',
        description: 'Generic Description'
    },
    function(response) {
            $('#player').show('slow');     
        });
});

$('.video').click(function() {
    var id = $(this).attr('id');
    showVideo(id);
});

function showVideo(id)
{
    $('#placeholder').fadeOut('slow');

    $.ajax({
        type: 'GET',
        url: '//www.domain.com/facebook/2011/video-player/youtube.php',
        data: 'id='+ id,
        beforeSend: function() {
            $('#placeholder').html('<img src="../images/youtube-loader.gif" style="min-height:274px;"/>');
        },
        complete: function(){
            //something
        },
        success: function(data) {
            $('#placeholder').html(data);
            $('#placeholder').fadeIn('slow');
        }
    });
}
});
</script>

youtube.php

if (isset($_GET['id']))
{
    $arrayId = $_GET['id'];

    $url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    $body = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($body, true);

    $video = $data['data']['items'][$arrayId]['id'];
    $title = $data['data']['items'][$arrayId]['title'];
    $views = $data['data']['items'][$arrayId]['viewCount'];

    $description = $data['data']['items'][$arrayId]['description'];
    $description = str_replace('uk.opticalexpress.com', '', $description);
    $description = str_replace('-', '', $description);
    $description = trim($description);

    $embed = 'https://www.youtube.com/embed/'.$video;

    $message = "<div id='player'><iframe style='z-index:9999;'width='480' height='274' src='". $embed ."' frameborder='0' allowfullscreen wmode='Opaque'></iframe></div>\n";   
    $message .= "<div id='title' style='font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;'>". $title ."</div>\n";
    $message .= "<div id='views' style='color:#BBB; font-size:12px;'>". $views ." views</div>\n";
    $message .= "<div id='description' style='font-size:14px;'>". $description ."</div>\n";
    $message .= "<img src = '../images/share_button.png' class='share_button' id='". $arrayId."' style='cursor:pointer;'>";
    $message .= "<div id='hr' style='padding:5px 0 10px 0; float:left; border-bottom: 1px solid #96CC90; width:480px; margin:0 0 15px 0;'></div>\n";

    echo $message;

}

?>

如果有任何不清楚的地方,请告诉我,我会尽力更好地解释它。

I have a facebook iframe tab which has a few youtube videos on it which are pulled directly from a json response provided by the youtube api.

When you select a thumbnail the video is embedded at the top of the tab and can then be played.

Also displayed underneath the embedded video is the title, description, views and a share button.

At the moment the share information is the same for every video and I am racking my brains how to customise it for each video, even if it means making an ajax call.

Here's the related code:

$(document).ready(function() {

$('.share_button').live('click', function(e){
    // fade out video to stop it conflicting with dialogue box
$('#player').fadeOut('slow');
    e.preventDefault();
    FB.ui(
    {
        method: 'feed',
        name: 'generic name',
        link: 'generic link',
        picture: 'generic image',
        caption: 'generic caption',
        description: 'generic description'
    },
    function(response) {
        // fade video back in once the dialogue box is closed
        $('#player').show('slow');     
    });
});

The complete code for my tab is:

$url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);

?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId : 'XXXXXXXXX',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml : true // parse XFBML
    });
};

(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>


<div id="placeholder" style="font-family:Helvetica; max-width:500px; margin:0 0 20px 0;">
<div id="player"><iframe width="480" height="274" src="https://www.youtube.com/embed/<?php echo $data['data']['items'][1]['id']; ?>" frameborder="0" allowfullscreen wmode="Opaque"></iframe></div>
<div id="title" style=" font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $data['data']['items'][1]['title']; ?> </div>
<div id="views" style="color:#BBB; font-size:12px;"><?php echo $data['data']['items'][1]['viewCount'];?> views</div>
<div id="description" style="font-size:14px;"><?php echo $data['data']['items'][1]['description'];?></div>
<img src = "../images/share_button.png" class="share_button" id="1" style="cursor:pointer;">
<div id="hr" style="padding:5px 0 10px 0; float:left; border-bottom: 1px solid #96CC90; width:480px; margin:0 0 15px 0;"></div>
</div>

<?php
$i = 0;

foreach($data['data']['items'] as $item) {
$id = $item['id'];
$thumb = $item['thumbnail']['sqDefault'];
?>
<div class="video" id="<?php echo $i; ?>" style="font-family:Helvetica; float:left; max-width:150px; min-height:130px; margin:0 0px 20px 10px; cursor:pointer;">
    <div class="thumb" style="position:relative; left:0; top:0;">
        <img src="<?php echo $item['thumbnail']['sqDefault'];?>" title="<?php echo $description;?>" style="position:relative; top:0; left:0px;"/>
        <img src="../images/play-thumb.png" style="position:absolute; top:30px; left:45px;"/>
    </div>
    <div class="title" style="font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;"><?php echo $item['title']; ?></div>
    <div class="views" style="font-size:12px; cursor:text;"><?php echo $item['viewCount'];?> views</div>
</div>

<?php
$i++;
}
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$('.share_button').live('click', function(e){
    $('#player').fadeOut('slow');
    e.preventDefault();
    FB.ui(
    {
        method: 'feed',
        name: 'Generic Name',
        link: 'Generic Link',
        picture: 'Generic Picture',
        caption: 'Generic Caption',
        description: 'Generic Description'
    },
    function(response) {
            $('#player').show('slow');     
        });
});

$('.video').click(function() {
    var id = $(this).attr('id');
    showVideo(id);
});

function showVideo(id)
{
    $('#placeholder').fadeOut('slow');

    $.ajax({
        type: 'GET',
        url: '//www.domain.com/facebook/2011/video-player/youtube.php',
        data: 'id='+ id,
        beforeSend: function() {
            $('#placeholder').html('<img src="../images/youtube-loader.gif" style="min-height:274px;"/>');
        },
        complete: function(){
            //something
        },
        success: function(data) {
            $('#placeholder').html(data);
            $('#placeholder').fadeIn('slow');
        }
    });
}
});
</script>

youtube.php

if (isset($_GET['id']))
{
    $arrayId = $_GET['id'];

    $url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=CHANNELID&v=2';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    $body = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($body, true);

    $video = $data['data']['items'][$arrayId]['id'];
    $title = $data['data']['items'][$arrayId]['title'];
    $views = $data['data']['items'][$arrayId]['viewCount'];

    $description = $data['data']['items'][$arrayId]['description'];
    $description = str_replace('uk.opticalexpress.com', '', $description);
    $description = str_replace('-', '', $description);
    $description = trim($description);

    $embed = 'https://www.youtube.com/embed/'.$video;

    $message = "<div id='player'><iframe style='z-index:9999;'width='480' height='274' src='". $embed ."' frameborder='0' allowfullscreen wmode='Opaque'></iframe></div>\n";   
    $message .= "<div id='title' style='font-size:14px; font-weight:bold; color:#3B5998; margin:2px 0 3px 0;'>". $title ."</div>\n";
    $message .= "<div id='views' style='color:#BBB; font-size:12px;'>". $views ." views</div>\n";
    $message .= "<div id='description' style='font-size:14px;'>". $description ."</div>\n";
    $message .= "<img src = '../images/share_button.png' class='share_button' id='". $arrayId."' style='cursor:pointer;'>";
    $message .= "<div id='hr' style='padding:5px 0 10px 0; float:left; border-bottom: 1px solid #96CC90; width:480px; margin:0 0 15px 0;'></div>\n";

    echo $message;

}

?>

If anything is unclear just let me know and I will try and explain it better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2024-12-28 17:47:01

如果您要共享粉丝页面上选项卡的链接,则无法自定义在 Facebook 上显示为描述的文本。他们默认使用页面的描述和 PAGE_NAME | TAB_TITLE 作为标题。

但是有一个解决方法:
您只需在服务器上共享一个 URL,即可在其中控制 Open Graph 标签并根据已共享的视频显示它们。如果用户现在点击此类共享链接,您只需将他们重定向到 Facebook 页面上的选项卡即可。

如果您还需要某种方式深层链接到共享视频,您可以使用 URL 参数“app_data”,该参数会传递到您的应用程序的 iFrame。

If you're sharing the link to the Tab on the Fan Page there is no way to customize the text that appears as the description on Facebook. They use the page's description by default and PAGE_NAME | TAB_TITLE as the title.

But there's a workaround:
You simply share a URL on your server where you can control the Open Graph tags and display them depending on the video that has been shared. If a user now clicks such a shared link, you can simply redirect them to the Tab on your Facebook Page.

If you also need some way of deep linking to the shared video, you can use the URL parameter "app_data" which get's passed to your App's iFrame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文