Skip to content
Mikaël DELSOL edited this page Jun 22, 2017 · 20 revisions

Basic Authentication

How do I generate a package using a WSDL protected by a Basic Authentication?
$ wget https://phar.wsdltophp.com/wsdltophp.phar
$ ./wsdltophp.phar generate:package \
    --urlorpath="http://www.mydomain.com/wsdl.xml" \
    --destination="/path/to/where/the/package/must/be/generated/" \
    --prefix="MyPackage" \
    ###############################################
    # Here are the Basic Authentication credentials
    --login="basic_auth_login" \
    --password="basic_auth_password" \
    ###############################################
    --force
How do I call a SOAP Web Service using a WSDL protected by a Basic Authentication?
use WsdlToPhp\PackageBase\AbstractSoapClientBase;
$options = array(
    AbstractSoapClientBase::WSDL_URL => 'http://www.mydomain.com/wsdl.xml',
    AbstractSoapClientBase::WSDL_CLASSMAP => \MyPackage\MyPackageClassMap::get(),
    ###############################################
    # Here are the Basic Authentication credentials
    AbstractSoapClientBase::WSDL_LOGIN => 'basic_auth_login',
    AbstractSoapClientBase::WSDL_LOGIN => 'basic_auth_password',
    ###############################################
);

PHP reserved keywords

How to avoid having fatal error with the operations?

If your Web Service provides a method named list, which is a PHP reseverd keywords, you don't have anything to do. Indeed, the generator takes care of generating a method with a unique name. You can view how it works here in the source code.

How to avoid having fatal error with the structs?

In case your Web Service provides structs named like PHP reserved keywords, the generator does not automatically rename the generated class as it can be hazardous.

To avoid having Structs named like PHP reserved keywords, you have two options which can be used independently or simultaneously:

  • prefix: this option prepends any generated Structs', and operations', name with the string you define. A Struct named List with prefix=Ws will be named WsList. You use the WsList classs and it will be send as List Struct thanks to the classmap SoapClient's option.
  • suffix: this option appends any generated Structs', and operations', name with the string you define. A Struct named List with suffix=Ws will be named ListWs. You use the ListWs classs and it will be send as List Struct thanks to the classmap SoapClient's option.

Namespacing

How do I use my own namespace?

If you need to namespace the generated classes, you can use the namespace option such as namespace='My\Own\Namespace'

How to have several packages under the same directory and common namespaces?

When you generate a package, you have to specify the destination folder. This folder at least contains a src folder with at most 4 sub folders: Arrays structs, Enums, Structs and Services. In addition, you may use a namespace to isolate each generated package. Nonetheless, you can use a common main folder to hold packages that share a same provider services version. Finally, namespace has to be defined in a composer.json file. The composer.json file is auto-generated by the generator at the same level of the generated src folder but it's only useful if you're working in this folder (--standalone is used as true by default). Otherwise you need to add the autoload path in your own composer.json file that leads to the generated package folder (--standalone can be set to false in this case). A practical case:

$ # first package
$ ./wsdltophp.phar [...] --namespace="Provider1/Service1" --destination="Provider1/Service1" --standalone=false
$ # second package
$ ./wsdltophp.phar [...] --namespace="Provider1/Service2" --destination="Provider1/Service2" --standalone=false
$ # third package
$ ./wsdltophp.phar [...] --namespace="Provider2/Service1" --destination="Provider2/Service1" --standalone=false
$ # fourth package
$ ./wsdltophp.phar [...] --namespace="Provider2/Service2" --destination="Provider2/Service2" --standalone=false

This generates something such as:

/Provider1
    /Service1
        /src
    /Service2
        /src
/Provider2
    /Service2
        /src
    /Service2
        /src

So you have to compose your composer.json such as:

"autoload": {
    "psr-4": {
        "Provider1\\Service1\\": "./Provider1/Service1/src/",
        "Provider1\\Service1\\": "./Provider1/Service2/src/",
        "Provider2\\Service1\\": "./Provider2/Service1/src/",
        "Provider2\\Service2\\": "./Provider2/Service2/src/"
    }
}

Proxy

How do I generate a package using a WSDL while I'm behind a proxy?

If you're behind a prox and you can't access the WSDL, then you can use the proxy's options:

$ wget https://phar.wsdltophp.com/wsdltophp.phar
$ ./wsdltophp.phar generate:package \
    --urlorpath="http://www.mydomain.com/wsdl.xml" \
    --destination="/path/to/where/the/package/must/be/generated/" \
    --prefix="MyPackage" \
    #############################
    # Here are the proxy settings
    --proxy-host="my.proxy.cache.proxy" \
    --proxy-port=7552 \
    --proxy-login="my_proxy_login" \
    --proxy-password="my_proxy_password" \
    #############################
    --force
How do I call a SOAP Web Service using a WSDL while I'm behind a proxy?
use WsdlToPhp\PackageBase\AbstractSoapClientBase;
$options = array(
    AbstractSoapClientBase::WSDL_URL => 'http://www.mydomain.com/wsdl.xml',
    AbstractSoapClientBase::WSDL_CLASSMAP => \MyPackage\MyPackageClassMap::get(),
    #############################
    # Here are the proxy settings
    AbstractSoapClientBase::WSDL_PROXY_HOST => 'my.proxy.cache.proxy',
    AbstractSoapClientBase::WSDL_PROXY_PORT => 7552,
    AbstractSoapClientBase::WSDL_PROXY_LOGIN=> 'my_proxy_login',
    AbstractSoapClientBase::WSDL_PROXY_PASSWORD => 'my_proxy_password',
    #############################
);

SoapClient

Can I use my own SoapClient class when generating the package?

No. Normally all the necessary options are provided by the generator to be able to reach the WSDL and its schemas. If not, please let us by creating an issue.

Can I use my own SoapClient class when sending requests with the generated package?

Yes. The generated package uses the decorator pattern in order to be able to provide additional utility methods on top of the SoapClient class. To learn more about the utility methods, please read the PackageBase README file. The PackageBase project contains all the base classes from which Structs (simple and array) and Operations classes inherit.

By default, the generated Operations classes inherit from the WsdlToPhp\PackageBase\AbstractSoapClientBase class. The name of the used class is an option that can be modified when you generate the package. The option soapclient gives you the availability to use your own SoapClient class representation. The only constraint, in order to make it work well, is that you must create at least one class. A class that:

To learn more how to do it, once again please read the PackageBase README file.

SoapHeaders

How do I set required SoapHeaders?

The required SoapHeaders are automatically detected by the generator. It means that when the operation is declared with headers such as:

<wsdl:operation name="RefundTransaction">
    <wsdlsoap:operation soapAction="" />
    <wsdl:input>
        <wsdlsoap:header message="ns:RequesterCredentials" part="RequesterCredentials" use="literal" />
        <wsdlsoap:body use="literal" />
    </wsdl:input>
    <wsdl:output>
        <wsdlsoap:header message="ns:RequesterCredentials" part="RequesterCredentials" use="literal" />
        <wsdlsoap:body use="literal" />
     </wsdl:output>
</wsdl:operation>

Then the generated Service class containing the operation RefundTransaction also contains the method that allows to set the required SoapHeader RequesterCredentials such as:

/**
 * Sets the RequesterCredentials SoapHeader param
 * @uses AbstractSoapClientBase::setSoapHeader()
 * @param \StructType\CustomSecurityHeaderType $requesterCredentials
 * @param string $nameSpace
 * @param bool $mustUnderstand
 * @param string $actor
 * @return bool
 */
public function setSoapHeaderRequesterCredentials(\StructType\CustomSecurityHeaderType $requesterCredentials, $nameSpace = 'urn:ebay:api:PayPalAPI', $mustUnderstand = false, $actor = null)
{
    return $this->setSoapHeader($nameSpace, 'RequesterCredentials', $requesterCredentials, $mustUnderstand, $actor);
}

Then you just have to write your code such as:

$options = array(
    \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => 'PayPalSvc.wsdl',
    \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => ClassMap::get(),
);
/**
 * Samples for Refund ServiceType
 */
$refund = new \ServiceType\Refund($options);
$requesterCredentials = new \StructType\CustomSecurityHeaderType(/* parameters */);
$refund->setSoapHeaderRequesterCredentials($requesterCredentials);
/**
 * Sample call for RefundTransaction operation/method
 */
if ($refund->RefundTransaction(new \StructType\RefundTransactionReq()) !== false) {
    print_r($refund->getResult());
} else {
    print_r($refund->getLastError());
}

And nothing more!

Http Headers

How do I set custom HTTP Headers?

Setting custom HTTP Headers is as easy as setting SoapHeaders. As soon as you have instanciated your Service class, you can call the setHttpHeader method such as:

$options = array(
    \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => 'PayPalSvc.wsdl',
    \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => ClassMap::get(),
);
/**
 * Samples for Refund ServiceType
 */
$refund = new \ServiceType\Refund($options);
$requesterCredentials = new \StructType\CustomSecurityHeaderType(/*[...])*/);
$refund->setSoapHeaderRequesterCredentials($requesterCredentials);
$refund->setHttpHeader('X-HEADER', 'X-HEADER-VALUE');
$refund->setHttpHeader('X-TOKEN', 'X-TOKEN-VALUE');
/**
 * Sample call for RefundTransaction operation/method
 */
if ($refund->RefundTransaction(new \StructType\RefundTransactionReq()) !== false) {
    print_r($refund->getResult());
} else {
    print_r($refund->getLastError());
}