1

I'm using the following function to send data to a particular 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.

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?

1 Answer 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 :

<?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.:

curl_setopt($ch, CURLOPT_ENCODING, "");
Sign up to request clarification or add additional context in comments.

14 Comments

Doesn't seem to work. I tried both of your suggestions. I'm not outputting to a browser
I appreciate your suggestions but I'm not seeing any differences in the output regardless what I do. If I remove CURLOPT_HTTPHEADER altogether I get an error that The server cannot service the request because the media type is unsupported
I honestly don't a see a reason for that to happen, utf8_decode should fix the encoding.
Maybe it's something else and not a Utf8 encoding issue. Maybe it's some other kind of encoding.
Upon further investigation utf8_decode changes text&lt;br&gt;⢠text to text&lt;br&gt;? text The real text should read text<br>• text
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.