JQuery, ajax and ampersand

I know that I should encodingURI any URL passed anything else because I read this: http://www.digitalbart.com/jquery-and-urlencode/

I want to share the current time of the current track that I am listening to. So I installed an excellent capper. And I have some code that combines all the bits and does the following: tracks = 2 & time = 967

Since I don’t want everyone to see my private key, I have a small php file that takes input and adds the following, so it looks like this: http: //myshorten.example/yourls-api.php? Signature = x & action = shorturl & format = simple & url = http: // urltoshorten? track = 2 & time = 967

So, on the main page, I call jquery $("div.shorturl").load(loadall);

Then it does a bit of CURL, and then the shortener returns a nice short URL.

Like this:

$myurl='http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=' . $theurl;
$ch = curl_init($myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}
echo $data;

Everything is perfect.

In addition, a shortened URL is always in the form http: // urltoshorten? Track = 2 — anything after the ampersand is shortened.

I tried to wrap the entire URL in php URLencode, I wrapped track = 2 & time = 967 in both encodeURI and encodeURIComponent, I tried to wrap it all with one or both. And yet, it breaks, although I can see that the submitted URL looks like track = 1% 26time% 3D5 at the end.

If I paste this or even the “normal” version with an unencrypted URL either into yoururls interface, or send it to yoururls via api, like a regular URL inserted into the browser’s location bar, it works fine again.

, , , URL- , , , CURL, ?

: " " ", ?". , , URL- ,

var track = $.getUrlVar('track');
var time = $.getUrlVar('time');

var, , * , - , , , , -.

- , .

+3
2

URL php URLencode

, (, URL- , URL- URL-). , URL, URL-, , , , URL- .

$myurl='http://...?...&url='.rawurlencode($theurl);

(urlencode() , rawurlencode() , , , [+ vs %20], rawurlencode() .)

URL, :

http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http%3A%2F%2Furltoshorten%3Ftrack%3D2%26time%3D967

. , yourls-api.php - .

+2

URL- php URLencode, = 2 & time = 967 encodeURI, encodeURIComponent, . , , , URL- track = 1% 26time% 3D5 .

, , HTTP, .

:

var1 = Bruce Oxford
var2 = Brandy&Wine
var3 = ➋➌➔            (unicode chars)

uri- var var, :

var1 = Bruce+Oxford
var2 = Brandy%26Wine
var3 = %E2%9E%8B%E2%9E%8C%E2%9E%94

, , charecters, :

?var1=Bruce+Oxford&var2=Brandy%26Wine&var3=%E2%9E%8B%E2%9E%8C%E2%9E%94

%3Fvar1%3DBruce+Oxford%26var2%3DBrandy%26Wine%26var3%3D%E2%9E%8B%E2%9E%8C%E2%9E%94

, , .

+1

Source: https://habr.com/ru/post/1760358/


All Articles