Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Coroutine\Http\Client->addFile(string $path, string $name, string $mimeType = null, string $fileName = null, int $offset = 0, int $length = 0): void
The absolute location of the file, cannot be an empty or non-existent file
Used for the name of the form for the HTTP FILES
parameter
Set the Mime content type format of the file like image/jpeg
, optional
Set the name of the file being sent, optional, will default to the file basename($path)
if not set
The offset of where to start from, allows you to upload parts of a file, useful for resumable transfers, default is the start of the file
The length of the file to send in relation to the offset, the default is the entire file
None
Attach files to the HTTP request. Allows you to send a POST request with a file.
Using this method will automatically use the POST HTTP method and change the header Content-Type
to form-data
. This method also has support for asynchronous file sending for large files, it is based on sendfile
.
<?php
use Swoole\Coroutine\Http\Client;
Co\run(function()
{
$client = new Client('127.0.0.1', 80);
$client->setHeaders([
'Host' => 'localhost',
]);
// Never timeout the request
$client->set(['timeout' => -1]);
$client->addFile('/var/www/example/files/cat.png', 'profileImage', 'image/png');
$client->post('/upload/image', ['age' => 23]);
echo $client->status . "\n";
var_dump($client->body);
$client->close();
});