1

I'm trying to imitate a POST request made by AJAX in PHP with cURL.

One thing I noticed, why does PHP escapes the single quote as %27 while JS's encodeuricomponent leaves it like it is?

Is there really a function in PHP that's actually THE SAME as the JS one?

Sandro Antonucci
  • 1,683
  • 5
  • 29
  • 59

1 Answers1

1

You're not going to find a PHP function that doesn't encode the single quote. Reason being is PHP adhere's more strictly to RFC 3986 which includes the single quote.

RFC 3986 reserves special characters such as !, ', (, ), and *.

URIs include components and subcomponents that are delimited by
characters in the "reserved" set. These characters are called
"reserved" because they may (or may not) be defined as delimiters by
the generic syntax, by each scheme-specific syntax, or by the
implementation-specific syntax of a URI's dereferencing algorithm.
If data for a URI component would conflict with a reserved
character's purpose as a delimiter, then the conflicting data must be percent-encoded before the URI is formed.

reserved = gen-delims / sub-delims

gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"

sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

If you want to match php and have the single quote encoded in js you can use a function like this.

function fixedEncodeURIComponent (str) {  
  return encodeURIComponent(str).replace(/[!'()*]/g, escape);  
} 

Why do you need to not encode it? It should still work with a CURL.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95