How to upload a video to Facebook event page via PHP

1.6k Views Asked by At

I have created an event on my test page and I'm going to upload a video to that event page from my web site. I have used the below code but it is doesn't work.

$data = [
       'title' => 'test Video',
       'description' => 'This is test video',
       'source' => $fb->videoToUpload($video_path),
];

try {
       $response = $fb->post('/' . $event_id . '/videos', $data, $page_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
       return 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e){
       return 'Facebook SDK returned an error: ' . $e->getMessage();
}

$graphNode = $response->getGraphNode();

I got this error.

Graph returned an error:(#33) This object does not exist or does not support this action.

So I fix some parts like this.

$data = [    
   .......
   'source' => $video_path
];
try{
   $response = $fb->post('/' . $event_id . '/feed', $data, $page_token);
}
.......

Then it works like this.

But I want to result like as this picture.

How shall I do?

1

There are 1 best solutions below

10
On

Official reference for post video on event (Api Graph v5.0):
Upload:

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.2',
  ]);

$data = [
  'title' => 'My Foo Video',
  'description' => 'This video is full of foo and bar action.',
  'source' => $fb->videoToUpload('/path/to/foo_bar.mp4'),
];

try {
  $response = $fb->post('/me/videos', $data, 'user-access-token');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$graphNode = $response->getGraphNode();
var_dump($graphNode);

echo 'Video ID: ' . $graphNode['id'];

POST ON EVENT:

/* PHP SDK v5.0.0 */
/* make the API call */
try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get(
    '/{event-id}/videos',
    '{access-token}'
  );
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();
/* handle the result */

If you see the result JSON :

{
    "data": [],
    "paging": {}
}

All reference:
-Start - Initialize an upload session
-Transfer - Upload video chunks
-Finish - Post the video

In the function:

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', '/.$idevent./videos', array(
                    'source' => new CURLFile('path', 'video/MOV'),
                    'message' => $formDataMessage,
                )
            ))->execute();
            $ajaxResponse = $response->getGraphObject();
        }
        return json_encode($ajaxResponse);
    }