Search code examples
phpsoapwsdlclient

How to create an arbitrary SOAP request using PHP?


I'm having a SOAP related problem in PHP. I'm trying to create arbitrary SOAP request using Nusoap_client class. The complete request including headers should look like the example below. Of course the placeholders (string) should be replaced with actual values.

POST /services/RecipeCouponAPI11.asmx HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.example.com/services/GetCouponAll"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <RCAuthenticator xmlns="http://www.example.com/services/">
      <UserName>string</UserName>
      <Password>string</Password>
    </RCAuthenticator>
  </soap:Header>
  <soap:Body>
    <GetCouponAll xmlns="http://www.example.com/services/">
      <campaignCode>string</campaignCode>
    </GetCouponAll>
  </soap:Body>
</soap:Envelope>

I have tried the following PHP code with no luck. Seems like the request isn't formatted correctly. Any ideas on how to get his working?

<?php
$client = new Nusoap_client('http://www.example.com/services/RecipeCouponAPI11.asmx');
$headers = new SoapHeader(
    'http://www.example.com/services/',
    'RCAuthenticator',
    (object)array(
        'UserName' => 'username',
        'Password' => 'password'
    ),
    false
);

$client->setHeaders(array($headers));

$response = $client->call(
    'GetCouponAll',
    array('campaignCode' => 'campaigncode'),
    null,
    'http://www.example.com/services/GetCouponAll'
);
?>

Solution

  • Found a solution after all.

    <?php
    $client = new SoapClient('http://www.example.com/services/RecipeCouponAPI11.asmx?wsdl');
    $header = new SoapHeader(
        'http://www.example.com/services/',
        'RCAuthenticator',
        array(
            'UserName' => 'username',
            'Password' => 'password'
        )
    );
    
    $client->__setSoapHeaders($header);
    
    $response = $client->GetCouponAll(array('campaignCode' => ''));
    ?>