robtimus/multipart
Composer 安装命令:
composer require robtimus/multipart
包简介
A library to support creating (streaming) multiparts
关键字:
README 文档
README
A library to support creating (streaming) multiparts.
Supported multipart types
multipart/form-data
To create a multipart/form-data object, create a MultipartFormData instance, add the form fields, and call finish(). There are two methods for adding form fields:
addValue($name, $value, $contentType = '', $contentTransferEncoding = '')adds a string value with the given name.$nameand$valueare required.$contentTypeis optional, and will be used for theContent-Typeheader if it is set. It should be set if the implicit content type should not betext/plain.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addFile($name, $filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a file with the given name.$name,$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
// the multipart object can take an optional pre-existing boundary $multipart = new MultipartFormData(); $multipart->addValue('name', 'Rob'); $multipart->addFile('file', 'file.txt', 'Hello World', 'text/plain'); // add a content type to a non-file part $multipart->addValue('json', '{"id": 1}', contentType: 'application/json') $multipart->finish();
Multiple values or files with the same parameter name
MultipartFormData follows RFC 7578, and not RFC 2388. This means that multiple values or files with the same parameter name are not sent with a multipart/mixed field but instead as separate parts.
PHP servers require multiple values or files to be sent with a name that ends with []. Because MultipartFormData is written to support also other server types that do not have this requirement, it is up to the caller to add these. For instance:
$multipart = new MultipartFormData(); $multipart->addValue('name', 'Rob'); $multipart->addFile('file[]', 'file.txt', 'Hello World', 'text/plain'); $multipart->addFile('file[]', 'file.html', '<html>Hello World</html>', 'text/html'); $multipart->finish();
multipart/related
To create a multipart/related object, create a MultipartRelated instance, add the root part and any inline files, and call finish(). There are two methods for adding parts:
addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part without a content disposition. This should be used for the root part.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addInlineFile($contentID, $filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds an inline file.$contentID,$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
// the multipart object can take an optional pre-existing boundary $multipart = new MultipartRelated(); $multipart->addPart(file_get_contents('body.html'), 'text/html'); // the content length is irrelevant because the content is a string // the content transfer encoding needs to be set to base64 $multipart->addInlineFile('logo', 'logo.png', base64_encode(file_get_contents('logo.png')), 'image/png', contentTransferEncoding: 'base64'); $multipart->finish();
To use this inline file in the HTML body, use cid:logo as the source of an image.
multipart/alternative
To create a multipart/alternative object, create a MultipartAlternative instance, add the alternatives, and call finish(). There are two methods for adding alternatives:
addMultipart(Multipart $multipart)adds another multipart as alternative. This is most often used with a multipart/related object.addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with the given content.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
// the multipart object can take an optional pre-existing boundary $multipart = new MultipartAlternative(); // $related is a MultipartRelated instance as created above $multipart->addPart(file_get_contents('body.txt'), 'text/plain'); $multipart->addMultipart($related); $multipart->finish();
multipart/mixed
To create a multipart/mixed object, create a MultipartMixed instance, add the parts, and call finish(). There are three methods for adding parts:
addMultipart(Multipart $multipart)adds another multipart. This is most often used with a multipart/alternative or multipart/related object.addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with the given content. This can be used for the bodies of plain text emails.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addAttachment($filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with content dispositionattachment.$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
// the multipart object can take an optional pre-existing boundary $multipart = new MultipartMixed(); // $alternative is a MultipartAlternative instance as created above $multipart->addMultipart($alternative); // the content length is irrelevant because the content is a string // the content transfer encoding needs to be set to base64 $multipart->addFile('file.png', base64_encode(file_get_contents('file.png')), 'image/png', contentTransferEncoding: 'base64'); $multipart->finish();
Multipart content
The content of a part or file can be given in one of three ways:
- As a string. The content length will be ignored.
- As a resource that can be read using
fread. It is up to the caller to close this resource. - As a callable that takes a length, and returns a string that is not larger than the given length. If there is nothing more to read it should return the empty string.
Examples, using a MultipartFormData object:
// content length is not necessary $multipart->addFile('file1', 'file.txt', 'Hello World', 'text/plain'); // make sure to close the resource after the request has been sent $resource = fopen('file.html'); $multipart->addFile('file.html', $resource, 'text/html', filesize('file.html')); // assume that class MyResource exists and has a function read($length) $myResource = new MyResource(...); $multipart->addFile('file.bin', array($myResource, 'read'), 'application/octet-stream');
cURL support
To send a multipart object with a cURL request, you need to follow some steps:
- Set the request type using
CURLOPT_CUSTOMREQUEST. - Set the
CURLOPT_UPLOADoption totrue. - Set the object's
curlReadmethod as theCURLOPT_READFUNCTION. - Make sure the
Content-TypeandContent-Lengthheaders are set. Note that theContent-Lengthheader is optional.
For instance:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_UPLOAD, true); curl_setopt($ch, CURLOPT_READFUNCTION, array($multipart, 'curlRead')); $headers = ['Content-Type: ' . $multipart->getContentType()]; $contentLength = $multipart->getContentLength(); if ($contentLength >= 0) { $headers[] = 'Content-Length: ' . $contentLength; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Non-streaming support
If streaming is not possible (e.g. because a string is required, like in the mail function), you can buffer a multipart object in-memory by calling the buffer method. This method takes an optional buffer size, and returns the buffered contents. The content length will be set accordingly. Note that you should do this before calling read (or curlRead), otherwise the buffered contents may not contain all desired contents (especially if you're using resources or callables).
Multipart.__toString() has been overridden to buffer the multipart object as well, so you can achieve the same by casting a multipart object to string. The difference is that buffer requires the multipart object to be finished.
robtimus/multipart 适用场景与选型建议
robtimus/multipart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.06M 次下载、GitHub Stars 达 14, 最近一次更新时间为 2018 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「streaming」 「multipart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 robtimus/multipart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robtimus/multipart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 robtimus/multipart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Multipart PSR-7 stream factories based on PSR-17 factories.
A middleware to support file uploads in GraphQL. Forked from ecodev/graphql-upload.
A modern, PSR-compliant HTTP client for PHP with middleware architecture, advanced retry strategies, and fluent API for building requests
A laravel bundle for the OpenTok PHP SDK
Laravel Wrapper for the OpenTok PHP SDK
A robust, configuration-driven ETL and data import framework for Laravel. Handles CSV/Excel streaming, queues, validation, and relationships.
统计信息
- 总下载量: 1.06M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 16
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-10-27