PHP XML以非UTF 8返回

时间:2022-10-24 20:48:42

I'm using the following function to send data to a particular API.

我正在使用以下函数将数据发送到特定的API。

function api_post($xml) {
    $ch = curl_init('http://api.asmx');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml;charset=UTF-8"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    $results = curl_exec($ch);
    return $results;
}

The output is

输出是

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <LoadResponse xmlns="http://api.api.com/">
         <LoadResult>
             <date>2015-09-18T10_07_51.997</date>
             <data><br>⢠bullet1<br>⢠Bullet2</data>
         </LoadResult>
      </LoadResponse>
   </soap:Body>
</soap:Envelope>

The results returned are as expected except that bullet points are returned as â¢and date values as 2015-09-18T10_07_51.997 instead of 2015-09-18T10:07:51.997.

返回的结果与预期一致,除了项目符号返回为原始值和日期值为2015-09-18T10_07_51.997而不是2015-09-18T10:07:51.997。

When I test out the same API call with the same XML in Soap UI everything is returned accurately. I'm assuming I have some kind of encoding issue in PHP. How can I resolve?

当我在Soap UI中使用相同的XML测试相同的API调用时,所有内容都会准确返回。我假设我在PHP中有某种编码问题。我该如何解决?

1 个解决方案

#1


0  

Depending on the remote encoding, you can use:

根据远程编码,您可以使用:

return utf8_encode($results);

You can also try

你也可以试试

return utf8_decode($results);

NOTE:

If you plan to output utf-8 to a browser, you can also use the following :

如果您计划将utf-8输出到浏览器,您还可以使用以下内容:

<?php
//Teel the browser we'll be outputting UTF-8
header('Content-Type: text/html; charset=UTF-8');
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');

Update based on you comment:

根据您的评论更新:

Try setting CURLOPT_ENCODING to "" (empty) and remove CURLOPT_HTTPHEADER, i.e.:

尝试将CURLOPT_ENCODING设置为“”(空)并删除CURLOPT_HTTPHEADER,即:

curl_setopt($ch, CURLOPT_ENCODING, "");

#1


0  

Depending on the remote encoding, you can use:

根据远程编码,您可以使用:

return utf8_encode($results);

You can also try

你也可以试试

return utf8_decode($results);

NOTE:

If you plan to output utf-8 to a browser, you can also use the following :

如果您计划将utf-8输出到浏览器,您还可以使用以下内容:

<?php
//Teel the browser we'll be outputting UTF-8
header('Content-Type: text/html; charset=UTF-8');
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');

Update based on you comment:

根据您的评论更新:

Try setting CURLOPT_ENCODING to "" (empty) and remove CURLOPT_HTTPHEADER, i.e.:

尝试将CURLOPT_ENCODING设置为“”(空)并删除CURLOPT_HTTPHEADER,即:

curl_setopt($ch, CURLOPT_ENCODING, "");