【问题标题】:how can I send ISO-8859-1 XML file using SoapClient?如何使用 SoapClient 发送 ISO-8859-1 XML 文件?
【发布时间】:2017-08-13 17:00:08
【问题描述】:
我正在使用一种 SOAP 方法,该方法需要使用 ISO-8859-1 编码的 XML 内容。我尝试将其直接添加为方法参数,但 SoapClient 返回编码错误:
SOAP 错误:编码:字符串 '(...)' 不是有效的 utf-8 字符串
如果我将 XML 内容转换为 UTF-8,SOAP 服务器将无法识别 XML 内容。
任何使用 SoapClient 的解决方案?
使用 cURL 的任何替代方法?
【问题讨论】:
-
根据官方文档,你不能,因为编码总是 utf-8 The encoding option defines internal character encoding. This option does not change the encoding of SOAP requests (it is always utf-8), but converts strings into it.: php.net/manual/en/soapclient.soapclient.php
标签:
php
xml
soap-client
iso-8859-1
【解决方案1】:
正如Constantin GALBENU 所指出的,它不能使用SoapClient 来完成。
我不得不改用 cURL,在内容类型标头发送字符集:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-type: text/xml;charset=iso-8859-1",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
));
$result = curl_exec($ch);
curl_close($ch);