Skip to content

Instantly share code, notes, and snippets.

@ilyaguy
Last active March 30, 2018 07:48
Show Gist options
  • Save ilyaguy/e183b6eacca1b0092b0d99b07df9bea7 to your computer and use it in GitHub Desktop.
Save ilyaguy/e183b6eacca1b0092b0d99b07df9bea7 to your computer and use it in GitHub Desktop.
guzzle post files with same name field
POST /post.php HTTP/1.1
Host: example.com
User-Agent: GuzzleHttp/6.3.2 curl/7.43.0 PHP/7.1.14
Content-Type: multipart/form-data; boundary=dbe860af679d39ed8d83b39cb2285a232429869c
Content-Length: 372
--dbe860af679d39ed8d83b39cb2285a232429869c
Content-Disposition: form-data; name="text"; filename="t1.txt"
Content-Length: 6
Content-Type: text/plain
file1
--dbe860af679d39ed8d83b39cb2285a232429869c
Content-Disposition: form-data; name="text"; filename="t2.txt"
Content-Length: 6
Content-Type: text/plain
text2
--dbe860af679d39ed8d83b39cb2285a232429869c--
<?php
require_once 'vendor/autoload.php';
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'http://example.com/post.php',
[
'multipart' => [
[
'name' => 'text',
'contents' => fopen('t1.txt', 'r'),
],
[
'name' => 'text',
'contents' => fopen('t2.txt', 'r'),
],
],
]
);
var_dump($response->getBody()->getContents());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment