-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
guzzle-lib.php
47 lines (37 loc) 路 1.13 KB
/
guzzle-lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$localFiles = [
'documents' => [
__DIR__ . '/resource/file-1.txt',
__DIR__ . '/resource/file-2.txt',
],
'images' => [
'icon' => __DIR__ . '/resource/github-icon.png',
'octocat' => __DIR__ . '/resource/github-octocat.jpg',
],
];
$uploadFiles = [];
foreach ($localFiles as $fileType => $files) {
foreach ($files as $filePostKey => $uploadFilePath) {
if (!file_exists($uploadFilePath)) {
throw new Exception('File not found: ' . $uploadFilePath);
}
$uploadFileMimeType = mime_content_type($uploadFilePath);
$keyName = sprintf('%s[%s]', $fileType, $filePostKey);
array_push($uploadFiles, [
'name' => $keyName,
'contents' => new SplFileObject($uploadFilePath, 'r'),
'filename' => $keyName,
]);
}
}
$httpClient = new Client();
$response = $httpClient->post(
'https://postman-echo.com/post',
[
RequestOptions::MULTIPART => $uploadFiles,
]
);
echo($response->getBody()->getContents());