Page 1 of 1

SOAP stumper

Posted: Fri Jun 08, 2007 1:19 am
by zkent
I am trying to consume a web service that requires a header object be inserted into the soap envelope:

This is what I am trying to create (created via WSDL by soapUI 1.7.1):

Code: Select all

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.traffic.com/schemas/v3_2/WebServiceClientContext" xmlns:com="http://webservices.traffic.com/schemas/v3_2/Commute">
   <soapenv:Header>
      <web:WebServiceClientContextElement>
         <web:ticket>big long authentication token</web:ticket>
      </web:WebServiceClientContextElement>
   </soapenv:Header>
   <soapenv:Body>
      <com:GetKeyRoutesPairsByMetroRequest>
         <com:metroId>18</com:metroId>
         <com:tags>WebsiteV2</com:tags>
      </com:GetKeyRoutesPairsByMetroRequest>
   </soapenv:Body>
</soapenv:Envelope>
This is what I have in PHP.

Code: Select all

<?php

// this is the authentication ticket I got from an authentication webservice
$ticket = "big long authentication token";

// array of values to be passed to method
$param = array(
   'metroId'  => '18',
   'tags'  => 'WebsiteV2'
);

try {

   // instantiate soapclient
   $client = new SoapClient("http://webservices.traffic.com/services/v3_2/Commute?wsdl");
   
   // create header object and insert into headers
   $auth = array('ticket'=>$ticket);
   $authvar = new SoapVar($auth, SOAP_ENC_OBJECT);
   $header =  new SoapHeader("http://webservices.traffic.com/schemas/v3_2/WebServiceClientContext",
                              "WebServiceClientContextElement", $authvar, false);
   
   $client->__setSoapHeaders(array($header));

   // do it
   print_r($response = $client->getKeyRoutesByMetro($param));
}
catch (SoapFault $exception) {
   echo $exception . "<br><br>";
}

?>
Running this produces this error.

Code: Select all

SoapFault exception: [soapenv:Server.userException] (null) in /home/zkent/public_html/communities/Traffic/common/development/roadwayWS/getKeyroutesByMetro.php:24
Stack trace:
#0 [internal function]: SoapClient->__call('getKeyRoutesByM...', Array)
#1 /home/zkent/public_html/communities/Traffic/common/development/roadwayWS/getKeyroutesByMetro.php(24): SoapClient->getKeyRoutesByMetro(Array)
#2 {main}
What stupid error am I making? I am at my wits end and the documentation on PHP and SOAP stinks.

Posted: Fri Jun 08, 2007 6:28 am
by volka
It's sending

Code: Select all

<ns2:WebServiceClientContextElement>
         <ticket>big long authentication token</ticket>
      </ns2:WebServiceClientContextElement>
ticket instead of ns2:ticket
I have no idea how to add the namespace prefix to ticket (and won't try since the classes SoapVar and SoapHeader drive me crazy).

Posted: Fri Jun 08, 2007 10:45 am
by zkent
OK I discovered the problem. It was all in the namespaces. First off, I discovered the trace=>true switch for the soapclient and added the __getLastRequest and __getLastResponse commands at the bottom (they didnt seem to work before without the debug turned on). Doing this allowed me to see the request and response SOAP messages. Adding the correct namespace to the SoapHeader allowed the __setSoapHeaders to format the XML correctly. Sigh. Thx for your help.

Code: Select all

<?php

$ticket = "big long authentication token";

$param = array(
   'metroId'  => '18',
   'tags'  => 'WebsiteV2'
);

try {
   $client = new SoapClient("http://webservices.traffic.com/services/v3_2/Commute?wsdl",
                            array('trace' => true));
   $auth = array('ticket'=>$ticket);

   $authvar = new SoapVar($auth, SOAP_ENC_OBJECT);
   $header =  new SoapHeader("http://webservices.traffic.com/schemas/v3_2/WebServiceClientContext",
                              "WebServiceClientContextElement",
                              $auth);
   
   $client->__setSoapHeaders(array($header));
   
   $client->getKeyRoutesByMetro($param);
}
catch (SoapFault $exception) {
   echo "EXCEPTION WAS: " . $exception . "<br><br>";
}

echo $client->__getLastRequest() . "<br><br>";
echo $client->__getLastResponse() . "<br><br>";

?>