gkd
gkd

Reputation: 863

Unable to upload video to user's facebook account using Graph API

I have created a facebook app in last month.

I am trying to upload a video from my PHP code, but it throws an error that (#353) You must select a video file to upload. While I tried to upload the same video from my Facebook account directly and it gets uploaded properly.

enter image description here

I don't know what is wrong things that exists, PHP code is as below

$api="/me/videos";
            $uploaded_videos=$facebook->api($api);

            $video_file_path=$user_dir_abs_path."/NewProject20.mov";

            if(file_exists($video_file_path))
            { 
                echo "file exists...";
            }else{
              die("not exist");
            }


             $ret_obj = $facebook->api('/me/videos', 'POST', array(
                'source' => '@' . $video_file_path,
                'title' => "This is just a test",
                'description' => 'test9000',
                'privacy' => json_encode(array('value' => 'EVERYONE' )),
                )
             );
             echo '<pre>'. $ret_obj.'</pre>';

Video I have uploaded is here

Document I refer to code is here

https://developers.facebook.com/blog/post/493/

https://developers.facebook.com/blog/post/608/

I have used following code as well, but I am getting that same error..

$id=$facebook->getUser(); /* UID of the connected user */

            $api="/".$id."/videos";
            echo "api -> $api";
             /*$ret_obj = $facebook->api('/me/videos', 'POST', array(*/
              $ret_obj = $facebook->api($api, 'POST', array(
                'source' => '@' . $video_file_path,
                'title' => "This is just a test",
                'description' => 'test9000',
                'privacy' => json_encode(array('value' => 'EVERYONE' )),
                )
             );
             echo '<pre>'. $ret_obj.'</pre>';

Upvotes: 1

Views: 2705

Answers (3)

maximus 69
maximus 69

Reputation: 1458

If anyone is still having issue uploading videos via the facebook sdk4 api, the below code worked for me:

$file_name = "/absolute/path/to/file/in/directory";
$linkData = [
  'source' => new \CURLFile($file_name, 'video/m4v'),
  'message' => 'Your message',
];
$ret_obj = $facebook->api('/me/videos', 'POST', $linkData);

Upvotes: 0

Ravi Dhoriya ツ
Ravi Dhoriya ツ

Reputation: 4414

From your comments, I got to know that you need to upload/post a video from your server to facebook, instead form posting method specified in documentation.

I don't know much about facebook-sdk, I would suggest you to use CURL method instead.

<?php
 $app_id = "XXXXXXXXXXXXXX";
 $app_secret = "XXXXXXXXXXXXXXXXXXXXXXX";
 $my_url = "http://localhost/url_of_this_page.php";
 $video_title = "Video title here";
 $video_desc = "Video description here";

 $code = $_REQUEST["code"];

 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/me/videos?"
 . "title=" . $video_title. "&description=" . $video_desc
 . "&". $access_token;

$ch = curl_init();
$data = array('name' => 'file', 'file' => '@'.realpath("ipad.mp4"));// use realpath
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);

if( $res === false ) {
    echo curl_error($ch);
}
curl_close($ch);
?>

As a response from facebooks, you'l get video id like:

{"id":"892161284142980"}

Upvotes: 4

Tobi
Tobi

Reputation: 31479

Have a look at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos/#publish

Videos must be encoded as multipart/form-data and published to graph-video.facebook.com instead of the regular Graph API URL.

Upvotes: 1

Related Questions