Altering Soap Headers in php

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
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

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"?>
    <ston:Authentication xmlns:ston="url1">
XML that im aiming for

Code: Select all


Post Reply