简体   繁体   中英

php - Facebook Video Upload Curl

To upload the video to Facebook using the following lines.

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

I received an error:

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

If I change curl to form post it works. Any ideas on why is it so?

Use the path to the video on server instead of the url. So:

$video = "uploads/video.mp4";

Then:

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

Notice the use of realpath() following the '@' symbol. Haven't tested with your code but I have a similar implementation and works great. Should do the trick!

For FB SDK4: (see the hardcoded video path, and the encoding).

FB requests the video file to be passed encoded as form-data: 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM