0

I'm tesitng a callback and it's adding unnecessary slashes which is impeding upon the API's ability to successfuly communicate with my server. Why is it appending these slashes and how can I remedy the situation? Code below:

<?php

$real_secret = 'cantletyousee';

$my_address = '16qVmcFj6EergwQuiXLqHucBC2BZzdpMfo';

$my_callback_url = 'http://myurlcantseeit.me/gg/blockchain_callback.php?secret=' . $real_secret;

$root_url = 'https://blockchain.info/api/receive';

$parameters = 'method=create&callback='. urlencode($my_callback_url) . '&address=' . $my_address;

$response = file_get_contents($root_url . '?' . $parameters);
var_dump($response);

$object = json_decode($response);

?>

The var dump is returning: string(203) "{"input_address":"1MtYhCDEq1euiV7PjL58XtjfwTgX3Bqqpe","callback_url":"http:\/\/myurlcantseeit.me\/gg\/blockchain_callback.php?secret=cantletyousee","fee_percent":0,"destination":"16qVmcFj6EergwQuiXLqHucBC2BZzdpMfo"}"

I replaced the secret and my site's URL with dummy information to keep it private - that won't make a difference.

As you can see, it should be http://myurlcantseeit.me/gg/blockchain_callback.php?secret=cantletyousee, but it's adding backslashes everywhere.

Why and how can I fix this?

Skylar White
  • 31
  • 1
  • 6
  • 2
    that is escape character `\\` . wont effect while you retrieve – xkeshav Oct 07 '13 at 05:14
  • Not seeing a problem here - http://codepad.viper-7.com/FcTPzu – Phil Oct 07 '13 at 05:14
  • What do you mean? Show ex. in code? – Skylar White Oct 07 '13 at 05:15
  • possible duplicate of [JSON: why are forward slashes escaped?](http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped) – Phil Oct 07 '13 at 05:16
  • @Phil it posts back to my URL and I'm not receiving any callbacks so it must be having an issue because of the backslashes. – Skylar White Oct 07 '13 at 05:16
  • @Phil not at all, completely different... – Skylar White Oct 07 '13 at 05:16
  • Hi Have you tried $object = json_decode($response); print_r($object); – Nes Oct 07 '13 at 05:17
  • @diEcho it's sending a request back to the URL http:\/\/myurlcantseeit.me\/gg\/blockchain_callback.php?secret=cantletyousee and I'm not receiving any callbacks so it must be something wrong with that. – Skylar White Oct 07 '13 at 05:17
  • 3
    @SkylarWhite The reasons for escaping forward-slashes are given in the answers to that duplicate question. There is no problem here as your JSON parser of choice should be able to decode those strings correctly (as seen in my codepad example above) – Phil Oct 07 '13 at 05:20
  • @SkylarWhite: How do you know that you haven't received the callback? Have you checked your apache access log? Maybe the problem is in the callback-script (blockchain_callback.php). – vstm Oct 07 '13 at 05:31

2 Answers2

4

\ is escape character , this won't affect your output.

From PHP escape sequences

The backslash character has several uses. Firstly, if it is followed by a non-alphanumeric character, it takes away any special meaning that character may have. This use of backslash as an escape character applies both inside and outside character classes.

Yes, you can add some options while using json_encode , give it a try, may helps you

json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
xkeshav
  • 53,360
  • 44
  • 177
  • 245
0

If you want to remove the backslashes, you can use str_replace()

$str2 = str_replace ('\\', '', $response);

By the way, I tried decoding your string with json_decode() and received the correct information (without the extra \) when doing var_dump():

class stdClass#1 (4) {
  public $input_address =>
  string(34) "1MtYhCDEq1euiV7PjL58XtjfwTgX3Bqqpe"
  public $callback_url =>
  string(72) "http://myurlcantseeit.me/gg/blockchain_callback.php?secret=cantletyousee"
  public $fee_percent =>
  int(0)
  public $destination =>
  string(34) "16qVmcFj6EergwQuiXLqHucBC2BZzdpMfo"
}
Sutandiono
  • 1,748
  • 1
  • 12
  • 21
  • 1
    why `str_replace` . escaping is essential. isn't it? – xkeshav Oct 07 '13 at 05:21
  • Yeah, it's still not working... Any idea why it might not be working? Docs: https://blockchain.info/api/api_receive – Skylar White Oct 07 '13 at 05:24
  • 1
    @diEcho: Yes, removing the backslashes is not essential, that's why I gave the sample output. Just added that in case OP wanted to remove it for whatever reason. @Skylar White: In the docs you provided, there is a parameter `shared=false`. Is that optional? – Sutandiono Oct 07 '13 at 05:28