Skip to content

WSDL to PHP with Basic Auth

There are a few ways to convert a WSDL file to PHP code with Basic Auth, but one common method is to use a tool called wsdl2phpgenerator. This tool generates PHP classes from a WSDL file. Authentication is handled at runtime when making SOAP requests, not during code generation.

First, you need to download the wsdl2phpgenerator package. Then you can use the command line to execute it and generate the PHP classes.

Command to generate PHP classes with wsdl2phpgenerator

bash
php wsdl2php.phar generate -i http://example.com/service.wsdl -o generated/ -n MyService

The above command generates PHP classes in the generated directory and uses MyService as the namespace. Basic Auth credentials are passed when instantiating the SOAP client in your PHP code.

After that, you can use the generated classes in your PHP code to make SOAP requests to the service.

You can also use other tools like wsdl2php (a command-line tool) or EasyWsdl (a library).

It is also possible to use SoapClient, which is a built-in PHP class to create a client for a web service, passing the WSDL URL and authentication information as arguments.

Example of using the SoapClient PHP class to create a client for a web service

php
<?php

$options = [
  'login' => 'user',
  'password' => 'pass',
  'trace' => 1,
  'exceptions' => 0,
];
$client = new SoapClient('http://example.com/service.wsdl', $options);

Example of using the generated classes with Basic Auth

php
<?php

require_once 'generated/MyService.php';

// Configure the underlying SoapClient with Basic Auth
$client = new \MyService\MyServiceSoapClient([
  'login' => 'user',
  'password' => 'pass',
]);

// Make a request using the generated method
$result = $client->SomeOperation();

Please note that the above examples assume that the WSDL and server support Basic Auth and your PHP version supports SOAP.

Dual-run preview — compare with live Symfony routes.