0

I have fetched some data from a url request using JSON with the following code:

$url = 'https://recruit.zoho.com/ats/private/xml/JobOpenings/getRecords?authtoken=$at&scope=recruitapi';
$request = new WP_Http;
$result = $request->request($url, $data = array());
$input = json_encode($result, true);
var_dump($input);

This code worked absolutely fine, except the data coming out looked really weird, such as:

"content-encoding":"gzip","vary":"Accept-Encoding","strict-transport-security":"max-age=15768000"},"body":"\u003C?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003E\n\u003Cresponse uri=\"\/ats\/private\/xml\/JobOpenings\/getRecords\"\u003E\u003Cresult\u003E\u003CJobOpenings\u003E\u003Crow no=\"1\"\u003E\u003CFL val=\"JOBOPENINGID\"\u003E\u003C![CDATA[213748000001263043]]\u003E\u003C\/FL\u003E\u003CFL val=\"Published in website\"\u003E\u003C![CDATA[false]]\u003E\u003C\/FL\u003E\u003CFL val=\"Modified by\"\u003E\u003C![CDATA

After some research, I realize that part of the problem most likely is the fact that there are æ, ø, and å in the data I'm requesting. Others have solved the problem this way:

$input = json_encode(utf8_decode($result), true);

However this gives me this error:

Warning: utf8_decode() expects parameter 1 to be string, array given in

I know the array is not a string, but how else do I deal with this? It seems to have worked for others, and I cant figure out why.

Thanks.

Edit:

I noticed this in the beginning of the printed data.

string(31486) "{"headers":{"server":"ZGS","date":"Wed, 12 Aug 2015 13:59:32 GMT","content-type":"text\/xml;charset=utf-8"

Does that mean it is already UTF-8 and I'm totally off?

Rasmus
  • 241
  • 3
  • 19
  • confused... exactly where is this `response uri = ...` coming from? That's the raw text returned by that url? or what you get after running it through json_decode? – Marc B Aug 12 '15 at 14:02
  • This is a snippet from the text caused by `var_dump($input)`. I'm sorry for any confusion, I'm quite new to both PHP and JSON. – Rasmus Aug 12 '15 at 14:04
  • that's not possible. `response uri =...` is not valid json. there's no way that json_encode() could ever produce a string like that. `"response uri =..."`, WITH the `"`-quotes, would maybe be valid json. – Marc B Aug 12 '15 at 14:05
  • Maybe some more context could help: `Accept-Encoding","strict-transport-security":"max-age=15768000"},"body":"\u003C?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003E\n\u003Cresponse uri=\"\/ats\/private\/xml\/JobOpenings\/getRecords\` – Rasmus Aug 12 '15 at 14:08
  • I also updated the snippet in the post, hope it clears things up a bit more. – Rasmus Aug 12 '15 at 14:10
  • 1
    `u003c` and `u003e` are `<` and `>`, so what you've got is escaped XML in a string. – Marc B Aug 12 '15 at 14:13

1 Answers1

1

What you receive in $result is an utf-8 string that seems to represent an url of some sort. Anyhow, json_encode will escape any unicode character to \u008E strings.

If you don't want to escape utf-8 character, this question is relevent to you : Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

Everything seems to work fine from what I see. Although, the string you have provided us seem to be troncated but I guess this is an error on your part.

Community
  • 1
  • 1
Nico
  • 6,395
  • 4
  • 25
  • 34
  • Hey, this seemed to change things a bit, but still can't see the æ ø å `"Modified time\\\"\\u003E\\u003C![CDATA[20 Jul-2015]]\\u003E\\u003C\\\/FL\\u003E\\u003CFL val=\\\"Job nr.\\\"\\u003E\\u003C![CDATA[93]]\\u003E\\u003C\\\/FL\\u003E\\u003CFL val=\\\"Stilling\\\"\\u003E\\u003C![CDATA[Senior Sales Manager]]\\u003E\\u003C\\\/FL\\u003E\\u003CFL val=\\\"Kunde\\\"\\u003E\\u003C![CDATA[Top Search Group]]\\u003E\\u003C\\\/FL\\u003E\\u003CFL val=\\\"Kundeansvarlig\\\"\\u003E\\u003C!` – Rasmus Aug 12 '15 at 14:21
  • I did the following: `$input = json_encode($result, true); $input_unicode = json_encode($input, JSON_UNESCAPED_UNICODE); var_dump($input_unicode);` – Rasmus Aug 12 '15 at 14:23
  • 1
    Why are you `json_encode` twice ? You only need to do it once. If you do it twice, everything will be ugly. just do `$input = json_encode($result, JSON_UNESCPAED_UNICODE);` Read the documentation carefully : http://se2.php.net/json_encode – Nico Aug 12 '15 at 14:25
  • Thanks! Got me even further, but now the body looks like this, I don't get it: `"\n<\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/row><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/FL><\/row>` – Rasmus Aug 12 '15 at 14:39