Created
January 31, 2019 11:55
-
-
Save egulhan/501ee8442824cf6aff272466844f2301 to your computer and use it in GitHub Desktop.
Request with Multipart data using Guzzle Http Client
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 | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\GuzzleException; | |
function uploadImage($requestUrl, $imageFilePath) | |
{ | |
$data = []; | |
try { | |
$client = new Client(['verify' => false]); | |
$result = $client->request( | |
'POST', | |
$requestUrl, | |
[ | |
'multipart' => [ | |
[ | |
'name' => 'type', | |
'contents' => 'banner', | |
], | |
[ | |
'name' => 'path', | |
'contents' => date('Y/m/d'), | |
], | |
[ | |
'name' => 'file_name', | |
'contents' => time() . '_' . rand(0, 99999), | |
], | |
[ | |
'name' => 'files[]', | |
// file content | |
'contents' => fopen($imageFilePath, 'rb'), | |
], | |
], | |
/* | |
'headers' => [ | |
# Do not override the Content-Type header here, Guzzle takes care of it | |
], | |
*/ | |
] | |
); | |
$data['result'] = true; | |
$data['response'] = (string)$result->getBody(); | |
} catch (\Exception $exception) { | |
$data['result'] = false; | |
$data['message'] = $exception->getMessage(); | |
} | |
return $data; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment