0

I am calling a web service that returns json text which ends up with some garbage at the start "". Any help or pointers appreciated. I am a bit rusty with the curl options and this from some old code i have used, it has been some time since I have done work like this.

When i call the web service through the browser i get nice json text, such as following. I have removed some of the values to make only a few lines

{ "values": [[1511596680,3],[1511596740,2],[1511596800,0],[1511596860,6],[1511596920,0],[1511596980,0],[1511597040,0],[1511597100,0],[1511597160,0],[1511603220,0],[1511603280,0],[1511603340,0],[1511603400,0],[1511603460,0],[1511603520,0],[1511603580,0],[1511603640,0],[1511603700,0],[1511603760,0],[1511603820,0]]}

when i call via a php page that acts as a wrapper. i get some crap in front of it, which is preventing php from calling json_decode on it. The called url is exactly the same that i used previously to call the web service in the browser.

{ "values": [[1511596680,3],[1511596740,2],[1511596800,0],[1511596860,6],[1511596920,0],[1511596980,0],[1511597040,0],[1511597100,0],[1511597160,0],[1511603220,0],[1511603280,0],[1511603340,0],[1511603400,0],[1511603460,0],[1511603520,0],[1511603580,0],[1511603640,0],[1511603700,0],[1511603760,0],[1511603820,0]]}

my php code to call the web service is as follows. I am not sure if $post_string being empty is a problem. The url consists of params passed in a url string in form ?param=val&param2=val2 etc.

$contenttype = 'application/json';

$headers = array(
       'Content-Type: ' . $contenttype,
       'Content-Length: ' . strlen($post_string) /* this an empty string */
        );

/* dump of headers 

Array
(
    [0] => Content-Type: application/json
    [1] => Content-Length: 0
)
*/

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // this is get */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (is_array($headers)
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch); // this contains the crap at the start */
Ab Bennett
  • 1,391
  • 17
  • 24

2 Answers2

0

i had insert the following to remive the Byte Order Mark.

$output = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $output);

thanks to the following link

How do I remove  from the beginning of a file?

Ab Bennett
  • 1,391
  • 17
  • 24
0

the "funny characters" are caused by the UTF-8 BOM, that means the string starts with EF BB BF signaling that it was encoded in UTF-8.

you can remove the BOM like this: (found in another answer, by jasonhao):

//Remove UTF8 Bom

function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}
musashii
  • 445
  • 6
  • 13