You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8中Guzzle发起SOAP POST请求遇500错误及空响应问题

解决方案

1. 修复XML标签不匹配问题

你的generateXml方法中存在明显的XML标签错误:<inf:getInvoicingTransactions>的闭合标签写成了</inf:getData>,这会导致XML格式非法,服务器解析失败直接返回500错误。

修改后:

// 原错误代码
$xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>2024-04-15</date></inf:getData>');

// 修正后
$xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>2024-04-15</date></inf:getInvoicingTransactions>');

2. 验证SOAPAction的正确性

你当前设置的SOAPAction值https://api.xyz.com/DataService/PostData与实际请求的服务地址不符,必须和Postman中使用的完全一致。通常SOAPAction对应WebService的方法命名空间+方法名,比如:

'SOAPAction' => 'http://infoservice.webservice.as24.com/getInvoicingTransactions'

3. 完善Content-Type请求头

添加字符集声明,确保与Postman的请求头一致:

'Content-Type' => 'text/xml; charset=utf-8'

4. 获取完整SOAP错误信息

修改catch块,打印服务器返回的完整错误响应,以便定位具体问题:

use GuzzleHttp\Exception\ClientException;

// ... 原有代码 ...

catch (ClientException $e) {
    $responseBody = $e->getResponse()->getBody()->getContents();
    dd($e->getCode(), $e->getMessage(), $responseBody);
} catch (Exception $e) {
    dd($e->getCode(), $e->getMessage());
}

5. 简化XML生成逻辑(可选)

原XML生成逻辑冗余且容易出错,建议直接拼接完整的SOAP信封:

public function generateXml($date)
{
    $dateStr = $date->format("Y-m-d");
    
    return <<<XML
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-16" 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>
    </soap:Header>
    <soap:Body>
        <inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/">
            <date>$dateStr</date>
        </inf:getInvoicingTransactions>
    </soap:Body>
</soap:Envelope>
XML;
}

6. 对比Postman请求的所有细节

开启Guzzle的调试模式打印完整请求,和Postman的请求逐一对比:

$client = new Client([
    'verify' => false,
    'debug' => true, // 打印完整请求日志
]);

检查请求头、请求体、编码等是否完全一致。


内容的提问来源于stack exchange,提问作者Jordi Tomàs Molina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 08:57:13