This tutorial is the explanation about publishing videos to Facebook
Obviously, the first thing to do is to download the PHP SDK: Facebook PHP SDK 4
You may also want to take a look at the basic tutorial from Facebook:
Getting started with the Facebook SDK for PHP
Alright, it is time for some code.
require_once 'sdk/autoload.php'; use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookResponse; use Facebook\FacebookSDKException; use Facebook\FacebookRequestException; use Facebook\FacebookAuthorizationException; use Facebook\GraphObject; FacebookSession::setDefaultApplication('API_KEY', 'API_SECRET'); /* * login helper with redirect_uri * make sure you have added the redirect uri to oauth list in your app */ $helper = new FacebookRedirectLoginHelper( 'http://example.com/push.php' ); // see if a existing session exists if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) { // create new session from saved access_token $session = new FacebookSession( $_SESSION['fb_token'] ); // validate the access_token to make sure it's still valid try { if ( !$session->validate() ) { $session = null; } } catch ( Exception $e ) { // catch any exceptions $session = null; } } if ( !isset( $session ) || $session === null ) { // no session exists try { $session = $helper->getSessionFromRedirect(); } catch( FacebookRequestException $ex ) { // When Facebook returns an error // handle this better in production code print_r( $ex ); } catch( Exception $ex ) { // When validation fails or other local issues // handle this better in production code print_r( $ex ); } } // see if we have a session if ( isset( $session ) ) { set_time_limit(0); //optional // save the session $_SESSION['fb_token'] = $session->getToken(); // create a session using saved token or the new one we generated at login $session = new FacebookSession( $session->getToken() ); // graph api request for user data $request = new FacebookRequest( $session, 'GET', '/me' ); $response = $request->execute(); // get response $meObject = $response->getGraphObject()->asArray(); // print profile data //echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>'; $request = new FacebookRequest( $session, 'GET', '/me/accounts' ); $response = $request->execute(); // get response $graphObject = $response->getGraphObject()->asArray(); // $page_id = $_POST['mypages']; echo 'Welcome: ' . $meObject['first_name'] ; if(isset($page_id) && $page_id > 0){ $video = 'input_video.mp4'; /* * Changes PHP's current directory to directory. */ chdir($_SERVER['DOCUMENT_ROOT'].'/uploads'); $access_token = $session->getToken(); foreach($graphObject['data'] as $account) { if( $account->id == $page_id ){ $access_token = $account->access_token; } } $post_params = array( 'title' => $_POST['title'], 'description' => $_POST['description'], 'name' => "file", 'source' => "@{$video}].mp4", 'access_token' => $access_token ); $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data")); curl_setopt($ch,CURLOPT_URL,'https://graph-video.facebook.com/'.$page_id.'/videos'); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); curl_setopt($ch, CURLOPT_TIMEOUT, 500000); //timeout in seconds curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); //execute post //{"id":"10153014281648499"} $result = curl_exec($ch); $json = json_decode($result); if(isset($json->id)){ echo '<h2>Congratulations! ID#('.$json->id.')</h2>'; } @unlink("{$video}.mp4"); }else{ echo '<form method="post" action="push.php">'; echo '<br><br>'; echo '<div style="float: left"><h4>Select Page</h4>'; echo '<select name="mypages">'; $i = 0; foreach($graphObject['data'] as $account) { echo ' <option value="'.$account->id.'">'.$account->name.'</option>'; $i++; } echo '</select>'; echo '<h4>Video Title</h4>'; echo '<input style="width: 270px; height: 30px" type="text" name="title" >'; echo '</div>'; echo '<div style="float: right; overflow: hidden"><h4>Video Description</h4>'; echo '<textarea style="width: 270px; height: 150px" name="description"></textarea></div>'; echo '<input style="margin-top: 10px" type="submit" value="Publish" >'; echo '</form>'; } } else { // show login url echo '<div><a href="' . $helper->getLoginUrl( array( 'email', 'user_friends','publish_pages','manage_pages','publish_actions' ) ) . '">Push to Facebook</a></div>'; }
Note: video duration should not exceed 45 minutes .