Created
December 12, 2017 18:18
-
-
Save malina-eb/46950a59f521b4569c7e19e4197f59e1 to your computer and use it in GitHub Desktop.
Make a multipart media upload to S3 using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$options = array( | |
'http'=>array( | |
'method'=>'GET', | |
'ignore_errors'=>true | |
) | |
); | |
$url = 'https://www.eventbriteapi.com/v3/media/upload/?type=image-event-logo&token=' . getenv('TOKEN'); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
$response = json_decode($result, true); | |
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true)); | |
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY; | |
define('FORM_FIELD', $response['file_parameter_name']); | |
$file_contents = file_get_contents('logo.png'); | |
$content = ''; | |
foreach ($response['upload_data'] as $key => $value) { | |
$content .= "--".MULTIPART_BOUNDARY."\r\n". | |
"Content-Disposition: form-data; name=\"" . $key . "\"\r\n\r\n". | |
$value . "\r\n"; | |
} | |
$content .= "--".MULTIPART_BOUNDARY."\r\n". | |
"Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename("logo.png")."\"\r\n". | |
"Content-Type: image/png\r\n\r\n". | |
$file_contents."\r\n"; | |
$content .= "--".MULTIPART_BOUNDARY."--\r\n"; | |
$options = array( | |
'http'=>array( | |
'method'=>'POST', | |
'ignore_errors'=>true, | |
'header'=>$header, | |
'content'=>$content | |
) | |
); | |
$url = $response['upload_url']; | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
var_dump($http_response_header); | |
$response = json_decode($result, true); | |
var_dump($response); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment