有没有可能添加一个下载链接,只有分享到Facebook才能下载文件?

8

这种情况可能吗?

客户访问我的网站,想要下载一份他们感兴趣的PDF技术文档,他们点击下载按钮,然后会出现一个Facebook分享窗口,要求他们登录并将其分享到Facebook上。一旦他们点击分享并在其个人主页上发布,那么下载就开始了?

非常感谢。

Ian


这将违反平台政策:“您不得激励用户使用(或将内容设置在使用Facebook社交渠道之后),或暗示奖励直接与我们的渠道使用相关。” - CBroe
2个回答

19

更新

根据Facebook的新政策,此行为已不被允许。使用需自负风险,我不承担任何责任。

是的,使用JavaScript SDK,它提供了response(现在不再提供)。 我们将创建一个if语句来查看响应是否具有post_id,如果是,则显示下载链接,否则执行其他操作(例如弹出警告框?)

演示 (API 2.0) (不可用;需要修订)

演示 (API 2.7) 可用 Rev#63

HTML

<div class="container">
    
    <div>
       <p>This file is locked, to unlock and download it, share it</p>
       <p class="hidden">Thanks for sharing, the file is unlocked and ready for download</p>
       <p class="hidden">In order to download this file, you need to share it</p>
    </div>

    <a class="fsl fsl-facebook" href="#" id="facebook-share">
       <span class="fa-facebook fa-icons fa-lg"></span>
       <span class="sc-label">Share on Facebook</span>
    </a>

    <a class="fsl content-download" href="#" id="download-file">
       <span class="fa-download fa-icons fa-lg"></span>
       <span class="sc-label">Download File</span>
    </a>
    
</div>

JavaScript(jQuery)

$('#ShareToDownload').on('click', function(e) {
            e.preventDefault();
            FB.ui({
                  display: 'popup',
                  method:  'share',
                  href:    location.href,
                  },
                  /** our callback **/
                  function(response) {
                          if (response && response.post_id) {
                          /** the user shared the content on their Facebook, go ahead and continue to download **/
                          $('#ShareToDownload').fadeOut(function(){ $('#downloadSection').fadeIn() });    
                          } else {
                          /** the cancelled the share process, do something, for example **/
                          alert('Please share this page to download this file'):
                  }
            });     
}); 

更新

随着API版本2.0的发布,Feed对话框已被弃用,并被新的现代分享对话框所取代,因此上述代码使用了新的分享对话框


非常感谢你,亚当!这太棒了! - Ian Walker
1
@Wilf,我相信这些方法在API2.5中仍然有效。不过,我会解决jsFiddle的问题并检查它是否正常工作。感谢您的提醒。 - Adam Azad
1
@Wilf,我刚收到jsFiddle的回复,他们已经解除了这个代码片段的阻止。而且,这个代码片段也进行了更新。请检查一下这个代码片段,注意你需要修改iframe源码以允许弹出窗口正常工作。这里是如何操作的:https://dev59.com/W1wY5IYBdhLWcg3w37CG#32721395 - Adam Azad
@AdamAzad,你是个天才!非常感谢你! - Wilf
1
@AdamAzad,你的脚本在我分享内容之前都运行得很好。但是分享后下载按钮就不显示了。请帮忙解决一下! - Wilf
显示剩余2条评论

2
谢谢,非常完美!我不知道如何从JSON文件中获取下载链接,所以我采用了稍微不太安全的方法。
将以下内容添加到响应检查部分。
$.post('optimus.php', { 'fieldname' : 'download', 'value' : 'yes'});

创建了一个新页面并设置了会话(optimus.php)。
<?php
    session_start();
    $_SESSION[$_POST['fieldname']] = $_POST['value'];
?>

Download.php 包含如下代码

<?php
session_start();
if($_SESSION['download'] ==  "yes"){
session_destroy();
$file = 'file.zip';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
} else {
    echo "You didn't share, or you already downloaded the file";
}
?>

因此,当用户分享某些内容时,$_SESSION ['download']会被设置为“yes”。Download.php检查它是否为“yes”,如果是,则自动开始下载。此外,会销毁该会话,因此他们只能下载一次。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接