Я использую следующую функцию для отправки данных в конкретный 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;
}
Выход
<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>
Возвращаемые результаты ожидаются, за исключением того, что точки 2015-09-18T10_07_51.997
возвращаются как значения â¢
и даты как 2015-09-18T10_07_51.997
вместо 2015-09-18T10:07:51.997
.
Когда я тестирую тот же API-вызов с тем же XML-интерфейсом в Soap UI, все возвращается точно. Я предполагаю, что у меня есть какая-то проблема с кодировкой в PHP. Как я могу решить?
В зависимости от удаленной кодировки вы можете использовать:
return utf8_encode($results);
Вы также можете попробовать
return utf8_decode($results);
ЗАМЕТКА:
Если вы планируете выводить 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');
Обновление на основе вашего комментария:
Попробуйте установить CURLOPT_ENCODING
на "" (пусто) и удалить CURLOPT_HTTPHEADER
, то есть:
curl_setopt($ch, CURLOPT_ENCODING, "");