以UTF-8发送的卷曲应为ISO-8859-1

时间:2014-02-05 17:40:13

标签: php curl encoding utf-8 character-encoding

发送文件:

$xml_file = 'myfile.xml';
$research_xml_url = "http://mydomain.com/receive.php";
$curl_resource = curl_init($research_xml_url);

curl_setopt($curl_resource, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($curl_resource, CURLOPT_ENCODING, 'ISO-8859-1'); // Doesn't seem to work
curl_setopt($curl_resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_resource, CURLOPT_POST, 1);
curl_setopt($curl_resource, CURLOPT_TIMEOUT, 360);
curl_setopt($curl_resource, CURLOPT_HEADER, 0);
curl_setopt($curl_resource, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl_resource, CURLOPT_URL, $research_xml_url);

$cwd = getcwd();
chdir($cwd);

$file_to_send = array('xml' => "@" . $cwd .'/'.  $xml_file);
curl_setopt($curl_resource, CURLOPT_POSTFIELDS, $file_to_send);
$incoming_xml = curl_exec($curl_resource);

if (curl_error($curl_resource)) {
    echo curl_error($curl_resource)  ;
} else {
    print_r($incoming_xml, TRUE);
}

$curl_arr = curl_getinfo($curl_resource);
print_r($curl_arr, TRUE);
curl_close($curl_resource);

它打印出来,说明UTF-8,但它结束的XML编码为ISO-8859-1():

Array
(
    [url] => http://mydomain.com/receive.php
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 216
    [request_size] => 273
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.029465
    [namelookup_time] => 4.5E-5
    [connect_time] => 0.005916
    [pretransfer_time] => 0.006021
    [size_upload] => 3990
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 135414
    [download_content_length] => 0
    [upload_content_length] => 3990
    [starttransfer_time] => 0.012331
    [redirect_time] => 0
)

这是接收脚本:

$postText = file_get_contents('php://input');
$outp = fopen("files/xmlfile" . date("YmdHis") . ".xml", "w");
fwrite($outp, $postText);
fclose($outp);

如何强制curl以ISO-8859-1的形式发送?

curl_setopt($curl_resource, CURLOPT_ENCODING, 'ISO-8859-1');

似乎无法正常工作

1 个答案:

答案 0 :(得分:0)

CURLOPT_ENCODING用于设置"Accept-Encoding:"标头,其值为“identity”,“deflate”和“gzip”。

如果要以特定编码发送XML数据,则需要将$research_xml_url从其当前编码转换为所需编码。例如,使用iconv()函数。