Altering Soap Headers in php

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
ewok1986
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Nov 08, 2017 2:45 am

Wed Nov 08, 2017 3:12 am

Hi All, new to the forum and php in general really. i have been tasked with consuming a soap web service in php and to be honest im not far off.

i just cant seem to create the correct xmlns in the soap-env header or root of my request.

is there anyway i can change xmlns:ns1 & xmlns:ns2 to what i need?

below is all code i currently have.

current php :

Code: Select all

<?php
class soap_request {
  private function securityheader($Secret, $ServiceUserId, $UtcTimestamp){	  
 
	$ns_ston = 'URL1';
	$ns_ston1 = 'URL2';
	$ns_ston2 = 'URL3';
	
    $root = new SimpleXMLElement('<root/>');
    $root->registerXPathNamespace('ston', $ns_ston);

	$root2 = new SimpleXMLElement('<root/>');
    $root2->registerXPathNamespace('ston1', $ns_ston1);

    $Authentication = $root->addChild('ston:Authentication', null, $ns_ston);
    $Authentication->addChild('ston:Secret', $Secret, $ns_ston);
    $Authentication->addChild('ston:ServiceUserId', $ServiceUserId, $ns_ston);
    $Authentication->addChild('ston:UtcTimestamp', $UtcTimestamp, $ns_ston);
	
    $auth = $root->xpath('/root/ston:Authentication')[0]->asXML();
	
    return new SoapHeader($ns_ston, 'Authentication', new SoapVar($auth, XSD_ANYXML), true);
  }

  function __construct($Secret, $ServiceUserId, $UtcTimestamp){
    //assuming this is the wsdl file
    $this->client = new SoapClient('serviceurl?WSDL', array('trace' => true,'soap_version' => SOAP_1_1));
    $this->client->__setSoapHeaders($this->securityheader($Secret, $ServiceUserId, $UtcTimestamp));
  }
}

$Secret_val = 'key1';
$ServiceUserId_val = 'key2';
$UtcTimestamp_val = '2017-11-06T15:30:34Z';

$service = new soap_request($Secret_val, $ServiceUserId_val, $UtcTimestamp_val);

$function_name = "GetContact";
$params = array("CRMType" => "StoneShot" , "Value"=> "123" );

try {
	$response = $service->client->__soapCall ($function_name, $params); 
} catch (Exception $e) {
   echo htmlentities($service->client->__getLastRequest()) . "\n";
}
	
?>
Current XML thats being generated from the above PHP

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:ns1="url2" 
 xmlns:ns2="url1">
  <SOAP-ENV:Header>
    <ston:Authentication xmlns:ston="url1">
      <ston:Secret>key1</ston:Secret>
      <ston:ServiceUserId>key2</ston:ServiceUserId>
      <ston:UtcTimestamp>2017-11-06T15:30:34Z</ston:UtcTimestamp>
    </ston:Authentication>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:GetContact/>
    <param1>123</param1>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML that im aiming for

Code: Select all

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ston="url1" 
xmlns:ston1="url2" 
xmlns:ston2="url3">
  <soapenv:Header>
    <ston:Authentication>
      <ston:Secret>?</ston:Secret>
      <ston:ServiceUserId>?</ston:ServiceUserId>
      <ston:UtcTimestamp>?</ston:UtcTimestamp>
    </ston:Authentication>
  </soapenv:Header>
  <soapenv:Body>
    <ston1:GetContact> 
      <!--Optional:-->
      <ston1:ContactID>
        <ston2:CRMType>?</ston2:CRMType>
        <ston2:Value>?</ston2:Value>
      </ston1:ContactID>
    </ston1:GetContact>
  </soapenv:Body>
</soapenv:Envelope>

Post Reply