简体   繁体   English

使用 PHP SoapClient 调用 XML Soap 失败

[英]Failed to call XML Soap using PHP SoapClient

I'm trying to call SOAP xml using PHP SoapClient.我正在尝试使用 PHP SoapClient 调用 SOAP xml。 However, I got the error of但是,我得到了错误

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC connection; org.springframework.jdbc.CannotGetJdbcConnectionException:无法获得 JDBC 连接; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 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.这是我使用 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.但是我已经使用 curl 成功地向服务器发送了 SOAP 请求。 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.在 try 块中,我将 xml 解析为一个数组。 However, this will only happen if the XML response return the intended data.但是,只有在 XML 响应返回预期数据时才会发生这种情况。 I want to get the message of the fault string of the XML of this.我想得到这个 XML 的故障字符串的消息。

<?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?有人可以帮我解析 XML 的故障错误或找到使用 SoapClient 成功连接的方法吗? Thank you.谢谢你。

I have found a way to get the error message of the xml.我找到了一种方法来获取 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"} {"faultstring":"身份验证失败","detail":"无效凭据"}

This may not be the best solution, but for now this works for me.这可能不是最好的解决方案,但现在这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM