簡體   English   中英

使用 PHP SoapClient 調用 XML Soap 失敗

[英]Failed to call XML Soap using PHP SoapClient

我正在嘗試使用 PHP SoapClient 調用 SOAP xml。 但是,我得到了錯誤

org.springframework.jdbc.CannotGetJdbcConnectionException:無法獲得 JDBC 連接; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

這是我使用 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);

但是我已經使用 curl 成功地向服務器發送了 SOAP 請求。 這是我的代碼。

$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);
}

在 try 塊中,我將 xml 解析為一個數組。 但是,只有在 XML 響應返回預期數據時才會發生這種情況。 我想得到這個 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>

有人可以幫我解析 XML 的故障錯誤或找到使用 SoapClient 成功連接的方法嗎? 謝謝你。

我找到了一種方法來獲取 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]]);

這將返回:

{"faultstring":"身份驗證失敗","detail":"無效憑據"}

這可能不是最好的解決方案,但現在這對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM