2012-02-28 147 views
0

我正在开发有一个功能,用户可以上传视频的Facebook的画布应用程序URL。如何上传视频,并得到它在Facebook应用程序

我想下面这个教程: Facebook Video upload documentation

但问题是,当用户上传的视频,我需要上传,这样我可以将其存储在之后获得的视频的URL从Facebook回我的数据库。我需要能够让我的应用程序的用户回放使用嵌入式播放器。

这可能吗?

如果不是你们可以推荐一些其他的解决办法?

回答

1

官方教程将帮助您了解什么,以及如何击中右图形API点上传视频。但显然你不能按原样使用它,因为你直接将表单提交到Facebook端点,因此你不再控制框架!

我已经写了基于从Facebook发布了类似的代码tutorial但使用卷曲,这样你就不会在Facebook上终点着陆。它用于上传图像。

使用相同的代码,但有轻微的变化,使其上传视频,而不是,你的代码可能看起来像:

<?php 
error_reporting(E_ALL & ~E_NOTICE); 
$app_id = "APP_ID"; 
$app_secret = "APP_SECRET"; 
$my_url = "REDIRECT_URI"; // mainly, redirect to this page 
$perms_str = "publish_stream"; 

$code = $_REQUEST["code"]; 

if(empty($code)) { 
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&scope=" . $perms_str; 
    echo("<script>top.location.href='" . $auth_url . "'</script>"); 
} 

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($my_url) 
. "&client_secret=" . $app_secret 
. "&code=" . $code; 
$response = file_get_contents($token_url); 
$p = null; 
parse_str($response, $p); 
$access_token = $p['access_token']; 
$graph_url= "https://graph-video.facebook.com/me/videos?" 
     . "access_token=" .$access_token; 
if (!empty($_FILES)) { 
    $params = array(); 
    if(isset($_POST['title'])) { 
     $params['title'] = trim($_POST['title']); 
    } 
    if(isset($_POST['description'])) { 
     $params['description'] = trim($_POST['description']); 
    } 

    $uploaddir = './uploads/'; // Upload folder 
    $uploadfile = $uploaddir . basename($_FILES['source']['name']); 
    if (move_uploaded_file($_FILES['source']['tmp_name'], $uploadfile)) { 
     $params['source'] = "@" . realpath($uploadfile); 
    } 

    // Start the Graph API call 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$graph_url); 

    /* 
     Next option is only used for 
     user from a local (WAMP) 
     machine. This should be removed 
     when used on a live server! 
     refer:https://github.com/facebook/php-sdk/issues/7 
    */ 
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    $result = curl_exec($ch); 
    $decoded = json_decode($result, true); 
    var_dump(curl_error($ch)); 
    curl_close($ch); 
    if(is_array($decoded) && isset($decoded['id'])) { 
     /* 
      video is uploaded successfully: 
      1) show success message 
      2) optionally, delete video from our server 
     */ 
     $msg = "Video uploaded successfully: {$decoded['id']}"; 
    } 
} 
?> 
<!doctype html> 
<html> 
<head> 
<title>Upload</title> 
<style> 
label {float: left; width: 100px;} 
input[type=text],textarea {width: 210px;} 
#msg {border: 1px solid #000; padding: 5px; color: red;} 
</style> 
</head> 
<body> 
<?php if(isset($msg)) { ?> 
<p id="msg"><?php echo $msg; ?></p> 
<?php } ?> 
<form enctype="multipart/form-data" action="" method="post"> 
    <p><label for="title">Title</label><input type="text" name="title" value="" /></p> 
    <p><label for="description">Description</label><input type="text" name="description" value="" /></p> 
    <p><label for="source">Video</label><input type="file" name="source" /></p> 
    <p><input type="submit" value="Upload" /></p> 
</form> 
</body> 
</html> 

而且这里有两点要注意:

  • 中提到的教程,这只是一个演示!你应该在现场应用程序中使用的代码!
  • 我不处理卷曲的错误在这里...但你应该!一个很好的起点将是官方的PHP-SDK code
  • 我猜你应该使用textarea为您的视频说明,而不是文本input
  • 你仍然需要user_videos权限读取的视频信息,并获取数据你需要
+1

非常感谢你 – ajaybc 2012-03-01 05:50:41

+0

@ajaybc欢迎你! – ifaour 2012-03-01 07:32:33

2

在Facebook的文件一看!

这里是一个链接,这将帮助你太:https://developers.facebook.com/docs/reference/api/video/

上传测试视频,并返回print_r一切。那里应该有一个视频ID。更新代码来获取视频ID,然后使用图形API来抓取有关视频的ID信息。它会返回这样的事情:

https://developers.facebook.com/tools/explorer/?method=GET&path=2031763147233

然后,你可以简单地抓取视频的embed_html部分和地方嵌入它。

抱歉,我不能有更多的帮助,也找不到有关视频API返回了任何信息,但我认为有一个ID。你必须测试才能发现。

+1

谢谢。但我发现@ifaour的回答更适合我的需求 – ajaybc 2012-03-01 05:51:32

相关问题