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?

user2029890
  • 2,493
  • 6
  • 34
  • 65

1 Answers1

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, "");
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Doesn't seem to work. I tried both of your suggestions. I'm not outputting to a browser – user2029890 Oct 11 '15 at 19:07
  • 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` – user2029890 Oct 11 '15 at 19:18
  • I honestly don't a see a reason for that to happen, `utf8_decode` should fix the encoding. – Pedro Lobito Oct 11 '15 at 19:20
  • Maybe it's something else and not a Utf8 encoding issue. Maybe it's some other kind of encoding. – user2029890 Oct 11 '15 at 19:21
  • Upon further investigation `utf8_decode` changes `text<br>⢠text` to `text<br>? text` The real text should read `text
    • text`
    – user2029890 Oct 11 '15 at 19:35
  • That does clean up the HTML tags, but still leaves the bullet point like this `
    â¢`
    – user2029890 Oct 11 '15 at 19:48
  • I guess you're receiving a xml response from the server, right ? Can you post a small example of the output?(update your answer) – Pedro Lobito Oct 11 '15 at 19:49
  • Thanks. I've updated the question. Note this is just a small snippet as there are many more elements returned but these two are the only areas where the values are not as expected. – user2029890 Oct 11 '15 at 20:01
  • Not sure what that did, but it not change the date issue and the bullet points looks like this `&lt;br&gt;` – user2029890 Oct 11 '15 at 20:12
  • XML special characters need to escaped, so it should be normal. – Pedro Lobito Oct 11 '15 at 20:13
  • Perhaps, but I'm not sure how that would help withe date formatting issue. – user2029890 Oct 11 '15 at 20:27
  • I wouldn't sweet to much about the date, you can use a regex to fix it – Pedro Lobito Oct 11 '15 at 20:30
  • The only reason I need it exact because I'm using the XML to send it right back to the server where it came from with slightly modified data. It keeps on rejecting it because of the date format. The frustrating part is it works perfectly in SoapUI – user2029890 Oct 11 '15 at 20:37
  • Use this to fix the time `$xml = preg_replace('/(\d)_(\d)/i', '$1:$2', $xml);` – Pedro Lobito Oct 11 '15 at 20:42