PHPackages                             php-soap/psr18-attachments-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-soap/psr18-attachments-middleware

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

php-soap/psr18-attachments-middleware
=====================================

Add SWA / MTOM attachments to your SOAP client

0.8.0(1mo ago)221.5k↓16.6%1MITPHPPHP ~8.4.0 || ~8.5.0CI passing

Since Jan 6Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/php-soap/psr18-attachments-middleware)[ Packagist](https://packagist.org/packages/php-soap/psr18-attachments-middleware)[ Fund](https://opencollective.com/php-soap)[ RSS](/packages/php-soap-psr18-attachments-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (40)Versions (12)Used By (0)

SOAP SWA / MTOM Middleware
==========================

[](#soap-swa--mtom-middleware)

This package provides the tools you need in order to add [SWA](https://www.w3.org/TR/SOAP-attachments/) or [MTOM](https://www.w3.org/TR/soap12-mtom/) Attachments to your PSR-18 based SOAP Transport.

Want to help out? 💚
===================

[](#want-to-help-out-)

- [Become a Sponsor](https://github.com/php-soap/.github/blob/main/HELPING_OUT.md#sponsor)
- [Let us do your implementation](https://github.com/php-soap/.github/blob/main/HELPING_OUT.md#let-us-do-your-implementation)
- [Contribute](https://github.com/php-soap/.github/blob/main/HELPING_OUT.md#contribute)
- [Help maintain these packages](https://github.com/php-soap/.github/blob/main/HELPING_OUT.md#maintain)

Want more information about the future of this project? Check out this list of the [next big projects](https://github.com/php-soap/.github/blob/main/PROJECTS.md) we'll be working on.

Installation
============

[](#installation)

```
composer require php-soap/psr18-attachments-middleware
```

This package includes the [php-soap/psr18-transport](https://github.com/php-soap/psr18-transport/) package and is meant to be used together with it.

Usage
-----

[](#usage)

### Attachments middleware

[](#attachments-middleware)

This middleware is used to add attachments to your SOAP request:

```
use Http\Client\Common\PluginClient;
use Soap\Psr18Transport\Psr18Transport;
use Soap\Psr18AttachmentsMiddleware\Middleware\AttachmentsMiddleware;
use Soap\Psr18AttachmentsMiddleware\Multipart\AttachmentType;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;

// You should store this attachment storage in a central place in your application e.g. inside a service container.
// It is used to store the attachments that are being sent and received.
$attachmentsStorage = new AttachmentStorage();

$transport = Psr18Transport::createForClient(
    new PluginClient($yourPsr18Client, [
        new AttachmentsMiddleware(
            $attachmentsStorage,
            AttachmentType::Swa // or AttachmentType::Mtom
        ),
    ])
);
```

This middleware will convert your regular SOAP request into a multipart SOAP request that contains the request attachments. A response that contains attachments will be converted back into a regular SOAP response whilst storing a copy of the attachments.

### Adding attachments

[](#adding-attachments)

Adding attachments to your request is done by using the `AttachmentsStorage` before sending your request to the SOAP server:

```
use Http\Client\Common\PluginClient;
use Phpro\ResourceStream\Factory\FileStream;
use Soap\Psr18Transport\Psr18Transport;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;

// You should store this attachment storage in a central place in your application.
// It is used to store the attachments that are being sent and received.
$attachmentsStorage = new AttachmentStorage();

$attachmentsStorage->requestAttachments()->add(
    Attachment::create(
        name: 'file',
        filename: 'your.pdf',
        content: FileStream::create('path/to/your.pdf', FileStream::READ_MODE),
    )
);
$yourSoapClient->request('Foo', $soapPayload);
```

### Receiving attachments

[](#receiving-attachments)

Receiving attachments is done by using the `AttachmentsStorage` after receiving your response from the SOAP server:

```
use Http\Client\Common\PluginClient;
use Phpro\ResourceStream\Factory\FileStream;
use Soap\Psr18Transport\Psr18Transport;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;

// You should store this attachment storage in a central place in your application.
// It is used to store the attachments that are being sent and received.
$attachmentsStorage = new AttachmentStorage();

$soapResponse = $yourSoapClient->request('Foo', $soapPayload);
$attachments = $attachmentsStorage->responseAttachments()

foreach ($attachments as $attachment) {
    $attachment->content->copyTo(
        FileStream::create('path/to/your/'.$attachment->filename, FileStream::WRITE_MODE)
    );
}
```

Encoders
--------

[](#encoders)

### XOP Includes

[](#xop-includes)

If you are using MTOM attachments in combination with [XOP](https://www.w3.org/TR/xop10/) you can use the `XopIncludeEncoder` to work directly with attachments from within your SOAP objects. This requires you to use the [php-soap/encoder](https://github.com/php-soap/encoder) pacakge:

```
composer require php-soap/encoder
```

```
use Soap\Encoding\EncoderRegistry;
use Soap\Psr18AttachmentsMiddleware\Encoding\Xop\XopIncludeEncoder

// You should store this attachment storage in a central place in your application.
// It is used to store the attachments that are being sent and received.
$attachmentsStorage = new AttachmentStorage();

EncoderRegistry::default()
    ->addComplexTypeConverter(XopIncludeEncoder::XMLNS_XOP, 'Include', new XopIncludeEncoder($attachmentsStorage));
```

This will allow you to use attachments directly from within your SOAP request and responses without the need of adding them to the `AttachmentStorage` manually:

```
use Phpro\ResourceStream\Factory\FileStream;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;

// Your request can now contain Attachments directly:
// These attachments will be automatically added to the AttachmentStorageInterface and a  element will be added to your request instead.
$yourSoapPayload = (object) [
    // A special cid named constructor is added to make sure your attachment Content-Id is cid spec-compliant and therefore can be used with XOP.
    'file' => Attachment::cid(
        uri: 'foo@domain.com',
        filename: 'your.pdf',
        content: FileStream::create('path/to/your.pdf', FileStream::READ_MODE)
    )
];

// If your resonse contains an  element, the AttachmentStorageInterface will automatically fetch the attachment and replace the  element with the actual attachment content:
$response = $yourSoapClient->request('Foo', $yourSoapPayload);
$response->foo->file->copyTo(FileStream::create('path/to/your.pdf', FileStream::WRITE_MODE));
```

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance90

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~63 days

Recently: every ~77 days

Total

8

Last Release

52d ago

PHP version history (3 changes)0.1.0PHP ~8.2.0 || ~8.3.0 || ~8.4.0

0.5.0PHP ~8.3.0 || ~8.4.0 || ~8.5.0

0.8.0PHP ~8.4.0 || ~8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0cb7e57de4834c7e94552864acb7c5640725b5ec0ea8f087ebbf6761dd3c206?d=identicon)[veewee](/maintainers/veewee)

---

Top Contributors

[![veewee](https://avatars.githubusercontent.com/u/1618158?v=4)](https://github.com/veewee "veewee (27 commits)")[![aldenw](https://avatars.githubusercontent.com/u/4245644?v=4)](https://github.com/aldenw "aldenw (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/php-soap-psr18-attachments-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/php-soap-psr18-attachments-middleware/health.svg)](https://phpackages.com/packages/php-soap-psr18-attachments-middleware)
```

###  Alternatives

[phpro/soap-client

A general purpose SoapClient library

8885.6M46](/packages/phpro-soap-client)[veewee/xml

XML without worries

1835.9M29](/packages/veewee-xml)[roave/backward-compatibility-check

Tool to compare two revisions of a public API to check for BC breaks

5953.3M56](/packages/roave-backward-compatibility-check)[php-soap/ext-soap-engine

An ext-soap engine implementation

443.2M7](/packages/php-soap-ext-soap-engine)[php-soap/wsdl

Deals with WSDLs

173.5M12](/packages/php-soap-wsdl)[php-soap/wsdl-reader

A WSDL reader in PHP

212.3M9](/packages/php-soap-wsdl-reader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
