JoseCarlosPB
JoseCarlosPB

Reputation: 875

How can I add a json as part of a url?

The question seems quite simple, but I've tried everything that I've read and nothing worked I have this example url: localhost/test/{"user":"test","password":"test"} so, that json is part of the url how can I add it? I tried the following things

 $arrayVariable =  array (
        "Usuario"  => "user",
        "Clave" => "test"

    );
    $res = json_encode($arrayVariable);

but the answer of $res is the following

"{\"Usuario\":\"user\",\"Clave\":\"test\"}"

I've tried str_replace to remove backslashed but it didn't work, I tried the following two functions

$res  = str_replace("\\","",$res)
$res = str_replace("\\\\","",$res)

but it didn't work because seems the backslash is part of the " quote

Edit: I can't change the url because is an external API so nothing I can do that way

Upvotes: 0

Views: 68

Answers (1)

Nicolas
Nicolas

Reputation: 8695

You could use the urlencode() function.

$arrayVariable =  array (
        "Usuario"  => "user",
        "Clave" => "test"

    );
    $res = urlencode(json_encode($arrayVariable));

But i think you should re-think your logic since this is a very unusual thing to do.

Upvotes: 3

Related Questions