使用以下行将视频上传到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
以形成帖子,则可行。关于为什么会这样的任何想法?
答案 0 :(得分:0)
使用服务器上视频的路径而不是网址。所以:
$video = "uploads/video.mp4";
然后:
$data = array('name' => 'file', 'file' => '@'.realpath($video),
'access_token' => $access_token, '_title' => $video_title,
'description' => $video_desc);
注意在'@'符号后面使用realpath()。没有使用您的代码进行测试,但我有类似的实现,并且工作得很好。应该做的伎俩!
答案 1 :(得分: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);
}