Failed to call XML Soap using PHP SoapClient

Tags: , , ,



I’m trying to call SOAP xml using PHP SoapClient. However, I got the error of

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ” for connect URL ‘null’

This is my code for using SoapClient.

$headers = array(
    "POST /AGS/services/GetVIXNCD HTTP/1.1",
    "Host: d1.financial-link.com.my",
    "SOAPAction: "https://d1.financial-link.com.my/AGS/services/GetVIXNCD"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache", 
    "Content-Type: text/xml; charset=utf-8 ",
    "Authorization: Bearer " . $token, //to post from end user
    "Content-Length: " . $this->getXmlLength($request)
);

$client = new SoapClient('https://d1.financial-link.com.my/AGS/services/GetVIXNCD?wsdl', $headers);

$arrExtraParam = array (
    'paramIndicator' => $paramIndicator,
    'paramRemark' => $paramRemark,
    'paramValue' => $paramValue,
);

$params = array (
    'agentcode' => $agentcode,
    'arrExtraParam' => $arrExtraParam,
    'bizregno' => $bizregno,
    'compcode' => $compcode,
    'newic' => $newic,
    'oldic' => $oldic,
    'passportno' => $passportno,
    'regno' => $regno,
    'requestid' => $requestid,
    'signature' => $signature
);
$response = $client->__soapCall('getVixNcdResult', $params);

But I have successfully sent a SOAP request to the server using curl. This is my code.

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

if($result === false) {

    $err = 'Curl error: ' . curl_error($ch);
    curl_close($ch);
    print $err;

} else {

    curl_close($ch);

    try {

        $xml = simplexml_load_string($result);
        $xml->registerXPathNamespace('success', 'http://servlet');
        $resultAfter = (array) $xml->xpath('//success:getVixNcdReqReturn')[0];

    } catch (Exception $fault) {

        return response()->json($fault->getMessage());
        
    }

    return response()->json($resultAfter);
}

Within the try block, I parse the xml into an array. However, this will only happen if the XML response return the intended data. I want to get the message of the fault string of the XML of this.

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode xmlns:axis2ns15="http://schemas.xmlsoap.org/soap/envelope/">axis2ns15:Client</faultcode>
            <faultstring>Authentication Failure</faultstring>
            <detail>Invalid Credentials</detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

Can somebody help me to parse the fault error of the XML or find a way to successfully connect using SoapClient? Thank you.

Answer

I have found a way to get the error message of the xml.

$xml = simplexml_load_string($result);
$faultstring = (array) $xml->xpath('//faultstring')[0];
$detail = (array) $xml->xpath('//detail')[0];

return response()->json(['faultstring' => $faultstring[0], 'detail' => $detail[0]]);

This will then return:

{“faultstring”:”Authentication Failure”,”detail”:”Invalid Credentials”}

This may not be the best solution, but for now this works for me.



Source: stackoverflow