I have a simple PHP API endpoint. A possible request could look like this
api.php?v1=bob&v2=foo
Everything works fine, except when I try to transmit an URL as one of its values, which contains an ampersand:
url: www.bob.com/?v3=bacon&v4=apples
request: api.php?v1=www.bob.com/?v3=bacon&v4=apples&v2=foo
Now obviously this doesn't work, when I try to access v1
with $_GET["v1"]
I get www.bob.com/?v3=bacon
, the part &v4=apples
is missing because $_GET
thinks it's another parameter.
So I tried to encode the URL like so:
url = encodeURIComponent(url)
And transmitting it with jQuery to the endpoint with the following code:
$.get("api.php", { v1: url, v2: 'foo'}, function(data, status) {
...
});
It still doesn't work.
I assume that jQuery builds the query string as expected and when it gets to the endpoint I got the same mess as before.
What am I missing? How can I solve this problem?