How to upload a video to facebook using php sdk 3.2
We can upload video to Facebook personnel account or Facebook business page using PHP sdk . Here I am using PHP SDK version 3.2 for uploading process. For downloading PHP SDK go to the link https://developers.facebook.com/docs/php/gettingstarted/3.2.3 and then click download button.
The next step is create an Facebook App. To create an Facebook app go to the link https://developers.facebook.com/. we can login to this account using our Facebook account credentials.
- Go to the path My App >> Add a new app >> Website >> skip and create App id.
- After entering the click create app id button. We get an App id and secret key of an App.
- Then we have to find access token that never expires using App id and secret key of Facebook App that we created before.
- https://www.facebook.com/dialog/oauth?client_id=[APPLICATION_ID]
&scope=manage_pages,publish_stream
&response_type=token
&redirect_uri=https://www.facebook.com/connect/login_success.html - We get the url like this https://www.facebook.com/connect/login_success.html#access_token=[ACCES…] as a success response.Take the [ACCESS_TOKEN] from there.But this acess token is expired one, we can genearate none expired access token using this acess token.
- https://graph.facebook.com/oauth/access_token?client_id=[APPLICATION_ID]
&client_secret=[APPLICATION_SECRET]
&grant_type=fb_exchange_token
&fb_exchange_token=[ACCESS_TOKEN].You can get access_token=[EXTENDED_ACCESS_TOKEN]. - https://graph.facebook.com/me/accounts?access_token=[EXTENDED_ACCESS_TO…]
- https://www.facebook.com/dialog/oauth?client_id=[APPLICATION_ID]
Find the code to upload video to Facebook in php below. Please ensure to include facebook.php file from PHP sdk. If you want to upload video to personal Facebook account post type should be like this '/me/videos'.If you want to upload video to Facebook page post type like this '/$pageid/photos'. You can find your facebook page id in an About tab of particular Facebook page.
require 'src/facebook.php'; $file = "video.mp3"; $post_type = '/me/videos'; $post_params = array( 'access_token' => "Acess token of facebook App", 'title' => 'title of the image', 'source' => '@' . $fil , ); $facebook = new Facebook(array( 'appId' => 'Facebook app id', 'secret' => 'Facebook secret key' )); $facebook->setFileUploadSupport(true); try{ $fbpost = $facebook->api($post_type, 'POST', $post_params); print($fbpost); } catch (Exception $e) { echo $e->getMessage(); }
The response of the code is the id of the uploaded video.