为了账号安全,请及时绑定邮箱和手机立即绑定

PHP Post XML 使用 CURL 编码问题

PHP Post XML 使用 CURL 编码问题

PHP
临摹微笑 2021-10-15 16:55:53
我正在尝试使用带有阿拉伯字符的 PHP 发送 XML CURL 请求,但存在编码问题!我正在使用以下脚本:    $input_xml = '<?xml version="1.0" encoding="utf-8"?><request><Name>تجربة رسالة</Name></request>';$headers = "charset=utf-8; Content-type: text/xml";$url = "http://www.test.com";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 10);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);$data = curl_exec($ch);curl_close($ch);一旦我尝试使用以下命令从服务器终端发出请求,编码就正确了:echo '<?xml version="1.0" encoding="UTF-8"?><request><Name>تجربة النص</Name></request>' | curl -X POST -H 'Content-type: text/xml; charset=utf-8' -d @- http://test.com请您建议应该对 PHP 代码进行哪些更新,以便编码工作?
查看完整描述

1 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

改变这一行:

$headers = "charset=utf-8; Content-type: text/xml";

到:

$headers = array("Content-type:text/xml; charset=utf-8");

它应该工作。


查看完整回答
反对 回复 2021-10-15
  • 1 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信