0

I'm a complete newb to PHP programming but have been tasked with writing an example PHP web services client that consumes my company's web service that is written in Java. We have two types of services. Those that require authentication and those that don't. I've used the wsdl2php library to successfully communicate with our services that don't need authentication. Our secure services require that I add a SOAP header to my request that looks like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="https://webservices.averittexpress.com/SendWebImageService"> 
    <soapenv:Header xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
        <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn"> 
            <Username>xxxxxxxx</Username> 
            <Password>xxxxxxxx</Password> 
        </ns:authnHeader> 
    </soapenv:Header> 

The problem I've got is that I have no idea how to do this with wsdl2php or if it is even possible. Here's the PHP file I'm currently using to test.

<?php

require_once 'LTLRateQuoteService.php';

$rqs = new LTLRateQuoteService();
$info = new ShipmentInfo();
$HeaderSecurity = array("authnHeader"=>array("UserName"=>"xxxxxx",
                                             "Password"=>"xxxxxx",));

$header[] = new SoapHeader("http://schemas.xmlsoap.org/soap/encoding",
                           "Header",$HeaderSecurity);

$rqs->__setSoapHeaders($header);

$args = new AvtLTLRateQuoteRequest();
$args->AccountNumber = '1234578';
$args->OriginCity = 'Cookeville';
$args->OriginState = 'TN';
$args->OriginZip = '38501';
$args->DestinationCity = 'Morristown';
$args->DestinationState = 'TN';
$args->DestinationZip = '37814';
$args->ShipDate = '12/12/2011';
$args->Customer = 'S';
$args->PaymentType = 'P';

$info->NumPieces = '1';
$info->NumHandling = '1';
$info->CubicFeet = '1';
$info->Items = '1';
$info->TotalItem = '1';
$info->TotalWeight = '100';
$args->ShipmentInfo = $info;

$grq = new getLTLRate();
$grq->arg0 = $args; 

$response = $rqs->getLTLRate($grq);
$details = $response->return;
print 'Total Freight Charge: ' . $details->TotalFreightCharge . ' ';
?>

I need to somehow attach the username and password to the header on this request and have no idea how to do it. I'm looked at the wsdl2php site and there's very little there in terms of documentation on how to use the library. Any help would be appreciated.

Thanks,

Andrew

neubert
  • 15,947
  • 24
  • 120
  • 212
Andrew Cooper
  • 723
  • 2
  • 14
  • 28
  • why complete newbs going into web services? Or maybe your chef is so clever to ask complete newb to write a web service? – s.webbandit Sep 15 '11 at 10:46
  • I'm not writing a web service. We're a Java house. We wrote the web service in Java. We have clients that use our service who use PHP to connect to it. I'm simply trying to write a simple client in PHP as an example of how to do it. I got the unsecured client to work fine. I'm just having troubles with the one that requires authentication. – Andrew Cooper Sep 15 '11 at 12:26
  • i have same problem with this can any one help me out – Jignesh.Raj Aug 02 '14 at 12:15

2 Answers2

2

Do not listen to the rude commenting trolls without the answers, only insults. here is how i did it.

function __construct($url='wsdl url')
 {
    $auth=array('AccountUsername'=>'test','AccountPW'=>'test');
    $authvalues = new \SoapVar($auth, SOAP_ENC_OBJECT);
    $header =  new \SoapHeader('http://www.nsurl.com/', 
                                'AuthenticationHeader', // Rename this to the tag you need
                                $authvalues, false);

    $this->soapClient = new SoapClient($url,array("classmap"=>self::$classmap,"trace" => true,"exceptions" => true));
    $this->soapClient->__setSoapHeaders(array($header));
    //set the Headers of Soap Client.
    //$this->soapClient->__setSoapHeaders($header);
}
0

Rather then using wsdl2php, would using PHP's in-house SOAP functions work better?

http://php.net/manual/en/class.soapclient.php

Drazisil
  • 3,070
  • 4
  • 33
  • 53