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

时间:2011-12-19 16:26:27

标签: php facebook facebook-php-sdk facebook-javascript-sdk

我有一个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;

}

?>

如果有什么不清楚,请告诉我,我会尝试更好地解释它。

1 个答案:

答案 0 :(得分:0)

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

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

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