用php-sdk graph api将视频上传到Facebook [英] Upload video to facebook with php-sdk graph api
本文介绍了用php-sdk graph api将视频上传到Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ post_url =https://graph-video.facebook。 COM / ME /视频?
。 title =。 $ VIDEO_TITLE。 & description =。 $ video_desc
。 &安培;。 $的access_token;
但是我想在Facebook php-sdk中执行此操作。
$ facebook-> api('/ me / videos /');
但似乎视频服务器是 https://graph-video.facebook.com 。
那么我如何使用php-sdk在图形API中执行此操作?
解决方案
通过图形更简单的
对于一个组,您可以这样做:
<?php
$ app_id =YOUR_APP_ID;
$ app_secret =YOUR_APP_SECRET;
$ my_url =YOUR_POST_LOGIN_URL;
$ video_title =TITLE FOR THE VIDEO;
$ video_desc =视频说明;
$ group_id =YOUR_GROUP_ID;
$ code = $ _REQUEST [code];
echo'< html>< body>';
如果(空($ code)){
$ dialog_url =http://www.facebook.com/dialog/oauth?client_id=
。 $ app_id。 & redirect_uri =。 urlencode($ my_url)
。 &安培;范围= publish_stream;
echo('< script> top.location.href ='$ dialog_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 =。 $代码;
$ access_token = file_get_contents($ token_url);
$ post_url =https://graph-video.facebook.com/。 $ group_id。 /视频?
。 title =。 $ VIDEO_TITLE。 & description =。 $ video_desc
。 &安培;。 $的access_token;
echo'< form enctype =multipart / form-dataaction ='$ post_url。'
method =POST>';
echo'请选择一个文件:';
echo'< input name =filetype =file>';
echo'< input type =submitvalue =Upload/>';
echo'< / form>';
echo'< / body>< / html>';
?>
到您可以执行的页面
<?php
$ app_id =YOUR_APP_ID;
$ app_secret =YOUR_APP_SECRET;
$ my_url =YOUR_POST_LOGIN_URL;
$ video_title =TITLE FOR THE VIDEO;
$ video_desc =视频说明;
$ page_id =YOUR_PAGE_ID; //将此设置为您的应用程序的APP_ID
$ code = $ _REQUEST [code];
echo'< html>< body>';
if(empty($ code)){
//获取用户发布到他们的页面的权限。
$ dialog_url =http://www.facebook.com/dialog/oauth?client_id=
。 $ app_id。 & redirect_uri =。 urlencode($ my_url)
。 &安培;范围= publish_stream,manage_pages;
echo('< script> top.location.href ='$ dialog_url。';< / script>');
} else {
//获取用户的访问令牌,所以我们可以GET / me / accounts
$ token_url =https://graph.facebook.com/ oauth / access_token?client_id =
。 $ app_id。 & redirect_uri =。 urlencode($ my_url)
。 & client_secret =。 $ app_secret
。 & code =。 $代码;
$ access_token = file_get_contents($ token_url);
$ accounts_url =https://graph.facebook.com/me/accounts? 。 $的access_token;
$ response = file_get_contents($ accounts_url);
//解析返回值并获取我们有
//访问的帐户数组。这在data []数组中返回。
$ resp_obj = json_decode($ response,true);
$ accounts = $ resp_obj ['data'];
//查找要发布视频的页面的访问令牌。
foreach($ accounts as $ account){
if($ account ['id'] == $ page_id){
$ access_token = $ account ['access_token'];
break;
}
}
//使用上面的页面访问令牌,创建我们的表单将用于上传视频的POST操作
//。
$ post_url =https://graph-video.facebook.com/。 $ page_id。 /视频?
。 title =。 $ VIDEO_TITLE。 & description =。 $ video_desc
。 &安培; =的access_token。 $的access_token;
//创建一个简单的表单
echo'< form enctype =multipart / form-dataaction ='$ post_url。'
method =POST >';
echo'请选择一个文件:';
echo'< input name =filetype =file>';
echo'< input type =submitvalue =Upload/>';
echo'< / form>';
}
echo'< / body>< / html>';
?>
I have seen an example in facebook documentation.
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
But i want to do this in facebook php-sdk.
$facebook->api('/me/videos/');
But it seems the video server is https://graph-video.facebook.com. So how do i do this in graph api using php-sdk?
解决方案
through graph its much more simple
To a group you can do this:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "TITLE FOR THE VIDEO";
$video_desc = "DESCRIPTION FOR THE VIDEO";
$group_id = "YOUR_GROUP_ID";
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo('<script>top.location.href="' . $dialog_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;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/" . $group_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
echo '</body></html>';
?>
to a page you can do this
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "TITLE FOR THE VIDEO";
$video_desc = "DESCRIPTION FOR THE VIDEO";
$page_id = "YOUR_PAGE_ID"; // Set this to your APP_ID for Applications
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
// Get permission from the user to publish to their page.
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream,manage_pages";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {
// Get access token for the user, so we can GET /me/accounts
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
$response = file_get_contents($accounts_url);
// Parse the return value and get the array of accounts we have
// access to. This is returned in the data[] array.
$resp_obj = json_decode($response,true);
$accounts = $resp_obj['data'];
// Find the access token for the page to which we want to post the video.
foreach($accounts as $account) {
if($account['id'] == $page_id) {
$access_token = $account['access_token'];
break;
}
}
// Using the page access token from above, create the POST action
// that our form will use to upload the video.
$post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&access_token=". $access_token;
// Create a simple form
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
}
echo '</body></html>';
?>
这篇关于用php-sdk graph api将视频上传到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文