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>';
?>
Related
I'm trying to setup this basic uploaded which allows a user to upload a video directly to my Facebook group but I'm having a problem with the user access token, its throwing back a error "Malformed access token"
I grabbed a user access token from the Facebook graph api explorer and hard coded it into the URL and everything worked fine
I have the slightest idea as to what the problem might be
MY CODE:
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "http://DOMAIN/post.php";
$video_title = "TITLE FOR THE VIDEO";
$video_desc = "DESCRIPTION FOR THE VIDEO";
$group_id = "GROUP";
$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_actions";
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?access_token=".$code."&"."title=".$video_title."&description=".$video_desc;
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>';
THE ERROR IS:
{
"error": {
"message": "Malformed access token AQDDR2mYCEP3F4MvRoBbHBhNViCPkdBhYu0IFyD3pkopzmIZlug69tzb3Cl7E1Z_5qzMTPBAnzMREGm0hU9Nym3EaIMCBelwhKOLwxSTQwStbZ7euPOxPLrfi9-JYSnJcjvy3K_13Ov14IaybzXfXhjNHLzZNuyofnFPbLVieYvOjfpT9UJSzS9TB6Plttbt0O4aKtnG9RkTaoNkJkwUgCw9IGV_dWw5vF77CrhyGxJ5B7quOZPjz39f5QtJ50X5njZE2C4jysMhg1Xfdg9vAhfaFYYEg4f_WrxVfF2QnHINHLLfhFbzSW74jJnEBoVszseA46nqWG4mnrgrs9K97jQ0iqc0UnCEY1JU92D94fX_kw",
"type": "OAuthException",
"code": 190,
"fbtrace_id": "DCS2/KjriCI"
}
}
I cleaned the access token and placed it in the post url and everything is working now
$app_id = "[APP_ID]";
$app_secret = "[APP_SECRET]";
$my_url = "[URL_TO_THIS_SCRIPT";
//$video_title = "TITLE FOR THE VIDEO";
//$video_desc = "DESCRIPTION FOR THE VIDEO";
$group_id = "[GROUP_OR_PAGE_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_actions";
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);
$access_token_Clean = strtr ($access_token, array ('{"access_token":"' => '', '","token_type":"bearer"}' => ''));
$post_url = "https://graph.facebook.com/".$group_id."/photos?access_token=".$access_token_Clean;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file" accept="image/*">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
echo '</body></html>';
The error I'm getting is:
Curl error: couldn't connect to hostCurl error: couldn't connect to
host
I have tried almost everything to solve this problem but it doesn't seem to work file_get_contents doesn't work either but its enabled.
$response = curl_exec($ch);
if($response === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
$response = $response;
}
curl_close($ch);
return $response;
}
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$fanpage_id ='325145047546976';
$post_login_url = "http://www.feriajanos.com/src/teszt.php";
$photo_url = "http://www.feriajanos.com/kepek/mas/posztok/poszt20130412104817.jpg";
$photo_caption = "Feri A János";
$code = $_REQUEST["code"];
if (!$code) {
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
if(isset($_REQUEST['code'] )) {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
$response = curl($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/me/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo curl($graph_url);
echo '</body></html>';
}
?>
I am using the following code it successfully upload video but after uploading it show some ID page. Can any one help how to redirect page to my desired location after uploading video successfully.
Thanks in advance.
$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";
$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 {
$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);
$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_url = "https://graph-video.facebook.com/" . $page_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&access_token=". $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>";
}
You will have to make the video upload to facebook happen on your server side, instead of your visitors browser, so you can catch the output and send the visitors browser to the right place.
Your users will upload their files on your server first, then you can use curl to send the video to facebook, and after its finished you can redirect the user.
Examples:
Facebook Graph api video object: http://developers.facebook.com/docs/reference/api/video/
Command line Curl examples: http://facebook.stackoverflow.com/questions/5227607/posting-an-embedded-video-link-using-the-facebook-graph-api
Example with Facebook PHP api: http://facebook.stackoverflow.com/questions/9018213/video-post-on-timeline-not-playing-inline
I am trying to get Facebook profile picture . I am using the following with my value in place of ######### . After running the PHP i get a message "Hello".
I want that i get my name also with it like "Hellow Aditya" along with my profile picture. what should i edit to get that ? Also would it be possible to save the profile picture on the sever itself?
<?php
$app_id = ###############;
$app_secret = "#################";
$my_url = "###################";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$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);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
You can get it this way:
http://graph.facebook.com/[USER ID]/picture
I am creating an app where the user can upload an image, that will be stored in apps profile page, and in the user profile. I'm using below code to do this work
<?php
$app_id = "XXXXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXXX";
$post_login_url ="XXXXXXXXXXXXXXXXXXXXXXX";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// first get your album id, let's assume you need to create it
// create this before hand and you can just reference the id
$attachment = array('access_token'=> ACCESS_TOKEN, 'name'=>$ablum_name);
try{
$album_resp = $facebook->api("/{$this->page_id}/albums", 'POST', $attachment);
}catch(Exception $e){
throw new Exception("Failed to create album: ". $e->getMessage());
}
$album_id = $album_resp['id'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/photos?"
. "access_token=" .$access_token;
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
}
?>
With this code it is going to the user's profile but not to the app's profile page. Can any one please tell what is wrong with it? Or is there another way to do it?