PHP Post XML 使用 CURL 编码问题

我正在尝试使用带有阿拉伯字符的 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 代码进行哪些更新,以便编码工作?


临摹微笑
浏览 147回答 1
1回答

尚方宝剑之说

改变这一行:$headers&nbsp;=&nbsp;"charset=utf-8;&nbsp;Content-type:&nbsp;text/xml";到:$headers&nbsp;=&nbsp;array("Content-type:text/xml;&nbsp;charset=utf-8");它应该工作。
打开App,查看更多内容
随时随地看视频慕课网APP