2

使用以下行将视频上传到 Facebook。

$video = "http://xxx.com/video.mp4";
$data = array('name' => 'file', 'file' => $video,
    'access_token' => $access_token,  '_title' => $video_title,
    'description' => $video_desc);
$post_url = "https://graph-video.facebook.com/" . $page_id . "/videos"; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);

我收到一个错误:

"error":{"message":"(#353) You must select a video file to
    upload.","type":"OAuthException","code":353}}

如果我更改curl为表单帖子,它会起作用。关于为什么会这样的任何想法?

4

2 回答 2

0

对于 FB SDK4:(请参阅硬编码的视频路径和编码)。

FB 请求将视频文件编码为表单数据传递: https ://developers.facebook.com/docs/graph-api/reference/user/videos/

private function postFBVideo($authResponse, $fileObj, $formData)
    {
        FacebookSession::setDefaultApplication('yourAppkey', 'yourAppSecret');
        $ajaxResponse = '';
        try {
            $session = new FacebookSession($authResponse->accessToken);
        } catch (FacebookRequestException $ex) {
            // When Facebook returns an error
            $ajaxResponse = 'FB Error ->' . json_encode($ex) ;
        } catch (\Exception $ex) {
            // When validation fails or other local issues
            $ajaxResponse = 'FB Validation Error - ' . json_encode($ex) ;
        }
        if ($session) {
            $response = (new FacebookRequest(
                $session, 'POST', '/me/videos', array(
                    'source' => new CURLFile('videos/81JZrD_IMG_4349.MOV', 'video/MOV'),
                    'message' => $formDataMessage,
                )
            ))->execute();
            $ajaxResponse = $response->getGraphObject();
        }
        return json_encode($ajaxResponse);
    }
于 2015-04-18T07:36:07.847 回答
0

使用服务器上视频的路径而不是 url。所以:

$video = "uploads/video.mp4";

然后:

$data = array('name' => 'file', 'file' => '@'.realpath($video),
'access_token' => $access_token,  '_title' => $video_title,
'description' => $video_desc);

请注意在“@”符号后面使用 realpath()。尚未使用您的代码进行测试,但我有类似的实现并且效果很好。应该做的伎俩!

于 2014-10-27T11:09:03.823 回答