Search code examples
phpxmlsoapsimplexml

PHP registerXPathNamespace() error


Fatal error: Call to a member function registerXPathNamespace() on a non-object in /home/gateway/public_html/index.php on line 83

So the error I am getting is above, and on that line is the following

$host = "services.incard.com.au/telechoicetransservice.asmx";

$timestamp = getGMTtimestamp();

$vars = 
"<?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/\">" .
    "<soap:Body>" .
        "<ProcessPayment xmlns=\"https://services.incard.com.au\">".
        "<auth>" .
            "<AccountCode>". urlencode($_POST['AccountCode'])."</AccountCode>".
            "<Username>". urlencode($_POST['AccountCode'])."</Username>".
            "<Password>". urlencode($_POST['AccountCode'])."</Password>".
        "</auth>" .
        "<MerchantNumber>".urlencode($_POST['MerchantNumber'])."</MerchantNumber>" .
        "<CustomerNumber>".urlencode($_POST['CustomerNumber'])."</CustomerNumber>".
        "<Amount>".urlencode($_POST['Amount'])."</Amount>".
        "<Description>".urlencode($_POST['Description'])."</Description>".
        "</ProcessPayment>".
"</soap:Body></soap:Envelope>";

$response = openSocket($host, $vars);

$xmlres = array();
$xmlres = makeXMLTree ($response);
$xml = simplexml_load_string($response); 
$xml->registerXPathNamespace('soap:Envelope', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//soap:Envelope:ResponseCode') as $item) {
    echo (string) $item; 
}
foreach ($xml->xpath('//soap:Envelope:ResponseDescription') as $item) {
    echo (string) $item; 
}

I can't figure out why it is not working.

The response we get back from the server that we are requesting is the following

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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/">
  <soap:Body>
    <ProcessPaymentResponse xmlns="https://services.incard.com.au">
      <ProcessPaymentResult>
        <ResponseCode>string</ResponseCode>
        <ResponseDescription>string</ResponseDescription>
      </ProcessPaymentResult>
    </ProcessPaymentResponse>
  </soap:Body>
</soap:Envelope>

Solution

  • Try replacing

    <?php
    $xml->registerXPathNamespace('soap:Envelope', 
                                       'http://schemas.xmlsoap.org/soap/envelope/');
    foreach ($xml->xpath('//soap:Envelope:ResponseCode') as $item) {
        echo (string) $item; 
    }
    foreach ($xml->xpath('//soap:Envelope:ResponseDescription') as $item) {
        echo (string) $item; 
    }
    ?>
    

    with

    <?php
    $xml->registerXPathNamespace( 'soap', 
                                    'http://schemas.xmlsoap.org/soap/envelope/' );
    $result = $xml->xpath('//soap:Body');
    
    foreach ($result as $body) {
      echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseCode . "<br />";
      echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseDescription . "<br />";
    }
    ?>
    

    Hope this helps.