My php code outputs / as \ /

I am sending an API call to GWN API network/create endpoint and my payload is an array as below;

$body_data = array(
            'networkName'           => 'BBC_55
            'country'                      => 'US',
            'timezone'                   => 'America/New_York',
            'networkAdministrators' => array(
                '12345',
                '13456'
            ),
            'cloneNetworkId'        => 02345
        );

When I echo $body_data the output is;

{“networkName”:“BBC_55”,“country”:“US”,“timezone”:“America\/New_York”,“networkAdministrators”:[“12345”,“13456”],“cloneNetworkId”:02345}

I need the timezone to be “America/New_York” NOT “America\/New_York” as is outputted.

How do I do that?

You can NOT use forward slashes like that. You need to ESCAPE them. Try this:

'timezone'                   => 'America\/New_York',

You need to do this inside data if you need to use it later on. You can use addslashes() function to add them where needed. You can remove them with stripslashes() function. But, you might want to read up on ESCAPEs. There are many ways to do that. For example, for Windows servers, you can use single \ and single / in folder lists. html/images is the same as html\images. But, not in a Linux server. When used in a database, it is preferable to use prepared statements which bypasses this issue. But, for strings, especially in arrays, use addslashes() and stripslashes() functions. In your last post of the output, how did you output that When you display dumps of strings with slashes of any type, you can use htmlentities( ) function to turn the slashes to their HTML equivalent…

Hope this helps and hope it did not confused you!

Thank you for your answer and additional support information!

I also found an answer that worked - I added JSON_UNESCAPED_SLASHES when encoding the data.

$body = json_encode($body_data, JSON_UNESCAPED_SLASHES);

Also unquoted the networkAdministrators array values and the call was successful.

Congrats! Always nice to solve a programming problem. See you in your next post…

Sponsor our Newsletter | Privacy Policy | Terms of Service