goetas-webservices / soap-client

PHP implementation of SOAP 1.1 and 1.2 client specifications
MIT License
175 stars 28 forks source link

How to pass credentials to client? #61

Closed etcware closed 2 years ago

etcware commented 2 years ago

I need to pass credentials to soap client, is there an example to do this?

goetas commented 2 years ago

you need to pass a custom message factory to the client factory.

someting like

use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;

// get your factory , as example $factory = new ClientFactory($metadata, $serializer);

$baseMessageFactory = Psr17FactoryDiscovery::findRequestFactory();

$myMessageFactory = new MyMessageFactory($baseMessageFactory);
$factory->setMessageFactory($messageFactory);

class MyMessageFactory implements RequestFactoryInterface
{
    private RequestFactoryInterface $parentFactory;
    public function __construct(RequestFactoryInterface $parentFactory): RequestInterface
    {
        $this->parentFactory = $parentFactory;
    }

    public function createRequest(string $method, $uri): RequestInterface
    {
        $message = $this->parentFactory->createRequest($method, $uri);
        $message = $message->withHeaders(...); // add here your auth headers as example
        return $message;
    }
}
etcware commented 2 years ago

Thanks, but I have an additional problem, I cannot generate the metadata because also to access to WSDL during metadata generation needs credentials...