Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created August 13, 2024 16:08
Show Gist options
  • Save soyuka/f52aa1a14f86bdf5c55767f5c686a7ba to your computer and use it in GitHub Desktop.
Save soyuka/f52aa1a14f86bdf5c55767f5c686a7ba to your computer and use it in GitHub Desktop.
Use facebook upload api with symfony http client (warning this endpoint is not yet supported by facebook's own endpoints)
$uploadSession = $this->metaClient->request('POST', sprintf('%s/uploads', $this->metaAppId), [
'query' => [
'file_name' => $mediaObject->getFilePath(),
'file_length' => $mediaObject->getContentSize(),
'file_type' => $mediaObject->getMimeType(),
'access_token' => $credential->getAccessToken(),
'appsecret_proof' => $this->secretProof($credential->getAccessToken()),
],
])->toArray();
if (!isset($uploadSession['id'])) {
throw new BroadcastServiceProviderException('Error while creating upload session');
}
$handle = $this->metaClient->request('POST', '/' . $uploadSession['id'], [ // / is important see https://github.com/symfony/symfony/issues/54332
'headers' => [
'file_offset' => '0',
'content-length' => $mediaObject->getContentSize(),
],
'body' => $this->mediaStorage->readStream($mediaObject->getFilePath()),
'query' => [
'access_token' => $credential->getAccessToken(),
'appsecret_proof' => $this->secretProof($credential->getAccessToken()),
],
])->toArray();
return $handle['h'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment