0

I'm very new to the SOAP service.

I got this error to implements into PHP.

Server was unable to process request. ---> Object reference not set to an instance of an object.

Code: Soap envelope (I retrieved it using Postman)

<?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/" 
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
  <soap:Body>
    <GetDataXMLbyID xmlns="http://tempuri.org/">
      <id>3133717</id>
      <datatypes>
        <AgentName>TEST</AgentName>
      </datatypes>
    </GetDataXMLbyID>
  </soap:Body>
</soap:Envelope>

Code: PHP

<?php  

class WsseAuthHeader extends SoapHeader {

    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    function __construct($user, $pass, $ns = null) {
        if ($ns) {
            $this->wss_ns = $ns;
        }

        $auth = new stdClass();
        $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

        $username_token = new stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
                new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns), SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }

}

$username = 'username';
$password = 'password';
$wsdl = 'url';

$wsse_header = new WsseAuthHeader($username, $password);

$client = new SoapClient($wsdl, array(
    "trace" => 1,
    "exceptions" => 0,
    'SOAPAction' => 'http://tempuri.org/GetXMLDatabyID',
    'header' => 'Content-Type: text/xml; charset=utf-8'
        )
);

$client->__setSoapHeaders(array($wsse_header));

$request = array(
    'GetXMLDatabyIDResult' => array(
        'id' => '313371',
        'AgentName' => 'TEST',
    )
);

$results = $client->getXMLDatabyID($request);

print_r($results);

I want to retrieve data like this into the PHP (Result)

<Name>JOHN PAUL CORNELIUS</Name>
<DOB>1980-11-12T00:00:00+08:00</DOB>

The problem is, I got no experience about SOAP Service in PHP, that's why I'm stuck. Anybody can help me out?

Thank you :)

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
rootnel
  • 43
  • 6
  • That error sounds like something .net/C# would spit out. Possible issue with the request structure or the server itself. – Scuzzy Oct 30 '17 at 03:35
  • I dont think so. But I can retrieve data using Postman. :/ – rootnel Oct 30 '17 at 03:55
  • @Scuzzy is right... First, it says there was a server error which would indicate the server failed; also keep in mind that doesn't sound like a PHP error. I also do not see anywhere you need to have the instance of an object... You functions do not require it – Sam Oct 30 '17 at 04:11

0 Answers0