مثال على تحميل الفيديو
يتناول هذا المثال تحميل فيديو ونشره على يوميات مستخدم ما باستخدام مجموعة Facebook SDK للغة PHP.
مثال
قبل تحميل الفيديو، راجع خيارات ومتطلبات نشر الفيديو لنقطة نهاية الفيديو المحددة التي تريد النشر إليها.
$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'];مثال على تحميل فيديو مقسم
في الإصدار 5.1 من مجموعة SDK، وفرنا ميزة التحميل المقسم لتحسين نسبة نجاح تحميل ملفات الفيديو الكبيرة. يمكن الاستفادة من هذه الوظيفة بطريقتين:
استدعاء كامل
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.3',
]);
$data = [
'title' => 'My Foo Video',
'description' => 'This video is full of foo and bar action.',
];
$path = '/path/to/foo_bar.mp4';
try {
$response = $fb->uploadVideo('me', $path, $data, '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'];تقسيم ذاتي
$accessToken = $accessToken ?: $this->defaultAccessToken;
$uploader = new FacebookResumableUploader($this->app, $this->client, $accessToken, 'v2.4');
$endpoint = '/'.$target.'/videos';
$file = $this->videoToUpload('/path/to/foo_bar.mp4');
$chunk = $uploader->start($endpoint, $file);
do {
// use existing implementation maxTriesTransfer or use your own code here
$chunk = $this->maxTriesTransfer($uploader, $endpoint, $chunk, $maxTransferTries);
} while (!$chunk->isLastChunk());
return [
'video_id' => $chunk->getVideoId(),
'success' => $uploader->finish($endpoint, $chunk->getUploadSessionId(), $metadata),
];