swoole_http_client->addFile
添加POST文件。
function swoole_http_client->addFile(string $path, string $name, string $filename = null, string $mimeType = null)
- $path 文件的路径,必选参数,不能为空文件或者不存在的文件
- $name 表单的名称,必选参数,FILES参数中的key
- $filename 文件名称,可选参数,默认为
basename($path)
- $mimeType 文件的MIME格式,可选参数,底层会根据文件的扩展名自动推断
使用addFile
只有POST的Content-Type
将变更为form-data
。addFile
底层基于sendfile
,可支持异步发送超大文件。
addFile在1.8.9或更高版本可用
使用示例
$cli = new swoole_http_client('127.0.0.1', 80); //post request $cli->setHeaders(['User-Agent' => "swoole"]); $cli->addFile(__DIR__.'/post.data', 'post'); $cli->addFile(dirname(__DIR__).'/test.jpg', 'debug'); $cli->post('/dump2.php', array("xxx" => 'abc', 'x2' => 'rango'), function ($cli) { echo $cli->body; });