无法使用Graph API将视频上传到用户的Facebook帐户

时间:2014-06-18 07:05:43

标签: php facebook facebook-graph-api video

我上个月创建了一个Facebook应用程序。

我正在尝试从我的PHP代码上传视频,但它会引发错误(#353) You must select a video file to upload.当我尝试直接从我的Facebook帐户上传相同的视频并正确上传时。

enter image description here

我不知道存在什么问题,PHP代码如下

$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>';

我上传的视频是here

文档我指的是代码在这里

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

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

我也使用了以下代码,但是我得到了同样的错误..

$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>';

3 个答案:

答案 0 :(得分:4)

根据您的评论,我知道您需要将视频从服务器上传/发布到Facebook,而不是文档中指定的表单发布方法

我不太了解facebook-sdk,我建议你改用CURL方法。

<?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);
?>

作为来自facebook的回复,您可以获得视频ID:

{"id":"892161284142980"}

答案 1 :(得分:1)

查看https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos/#publish

  

视频必须编码为multipart/form-data并发布到   graph-video.facebook.com代替常规的图谱API网址。

答案 2 :(得分:0)

如果有人仍然无法通过facebook sdk4 api上传视频,则以下代码对我有用:

$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);