我正在尝试使用带有阿拉伯字符的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代码进行哪些更新才能使编码有效?
答案 0 :(得分:1)
更改此行:
$headers = "charset=utf-8; Content-type: text/xml";
收件人:
$headers = array("Content-type:text/xml; charset=utf-8");
应该可以。