我试图在 Laravel 8 中使用 Soap 进行后调用,以获取一些数据,但是如果我在调用时放置标头,他的返回错误 500,我显示下面的代码,而且如果没有定义标头,则得到空响应
try {
$data = $this->generateXml($get_last_day);
$client = new Client(['verify' => false ]);
$headers = [
'SOAPAction' => "https://api.xyz.com/DataService/PostData",
'Content-Type' => "text/xml"
];
$get_invoices_only_paid = $client->request('POST', config('constants.service.URL'), [
'headers' => $headers,
'body' => $data
]);
Log::debug(json_encode($get_invoices_only_paid->getStatusCode()));
dd($get_invoices_only_paid->getStatusCode());
} catch (Exception $e) {
dd($e->getCode(), $e->getMessage());
}
我通过的xml肥皂:
public function generateXml($date)
{
// $xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>'.$date->format("Y-m-d").'</date></inf:getData>');
$xml = new SimpleXMLElement('<inf:getInvoicingTransactions xmlns:inf="http://infoservice.webservice.as24.com/"><date>2024-04-15</date></inf:getData>');
$customXML = new SimpleXMLElement($xml->asXML());
$dom = dom_import_simplexml($customXML);
$cleanXml = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
$soapHeader = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><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-ENV:Header><soap:Body>';
$soapFooter = '</soap:Body></soap:Envelope>';
$xmlRequest = $soapHeader . $cleanXml . $soapFooter;
return $xmlRequest;
}
以及调用后的错误:
500
"""
Server error: `POST https://services.as24.com:8445/infoservice/services/infoservice` resulted in a `500 500` response:\n
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</fau (truncated...)\n
"""
邮递员中的相同请求可以与相同的标头和正文数据完美配合
编辑: 现在我编辑了请求并返回 200 代码,并且 xml 响应修复了添加标头 'Connection' => 'keep-alive' 并直接从文件添加正文 xml
$data = $this->generateXml($get_last_day);
$client = new Client(['verify' => false ]);
$headers = [
// 'SOAPAction' => "https://api.xyz.com/DataService/PostData",
'Content-Type' => "text/xml",
'Connection' => 'keep-alive',
'Content-Length' => '816',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept' => '*/*',
];
$data = $data = file_get_contents('soap-request.xml');
$get_invoices_only_paid = $client->request('POST', config('constants.AS24.URL'), [
'headers' => $headers,
'body' => $data
]);
Log::debug(json_encode($get_invoices_only_paid));
Log::debug($get_invoices_only_paid->getBody());
Log::debug(json_encode($get_invoices_only_paid));
dd($get_invoices_only_paid->getStatusCode());
通过编辑请求添加标头 'Connection' => 'keep-alive' 并直接从文件添加 xml 正文来解决问题,可能是 php 生成 xml 的格式有问题,而 keep alive 允许保持调用直到返回数据,因为响应很大。
这是最终的代码:
try {
// $data = $this->generateXml($get_last_day);
$client = new Client(['verify' => false ]);
$headers = [
'Content-Type' => "text/xml",
'Connection' => 'keep-alive',
'Content-Length' => '816',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept' => '*/*',
];
$data = file_get_contents('soap-request.xml');
$get_invoices_only_paid = $client->request('POST', config('constants.AS24.URL'), [
'headers' => $headers,
'body' => $data
]);
$jsonFormated = $this->xmlToJson($get_invoices_only_paid->getBody());
} catch (Exception $e) {
dd($e->getCode(), $e->getMessage());
}