Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created January 31, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egulhan/501ee8442824cf6aff272466844f2301 to your computer and use it in GitHub Desktop.
Save egulhan/501ee8442824cf6aff272466844f2301 to your computer and use it in GitHub Desktop.
Request with Multipart data using Guzzle Http Client
<?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