Is there a way to use http_build_query()
without having it URL encode according to some RFC standard?
Why I don’t want to URL encode everything: I’m querying the Ebay API.. They honestly insist on parameter names not being URL encoded, as far as commas in parentheses. E.g. DomainName(0) is a parameter, and the query fails if those parens are encoded.
10
Answers
Nope, it appears to always want to encode (which it should, it is meant to URL encode when building a list of params for a URL).
You could make your own…
CodePad.
You might want to try their JSON API instead. I tried to get a working sample, but I don’t have an app name so I can’t verify the result. Here’s the code:
You’ll need to to fill
$appName
with whatever the name of the app is. Also theX-EBAY-SOA-OPERATION-NAME
will need to be set to the actual call, and the JSON modified if the call is different.my take on alex’s answer but is faster
You can use
urldecode()
function on a result string which you get fromhttp_build_query()
http_build_query() is INVALID without urlencoding. Maybe you accidentally double-encode it?
For example, try to build http query for this array:
Without encoding, you would get
When properly encoded, you would get
which is correct. If you skip encoding, be sure to avoid URL injection attacks.
This could also work
You can use urldecode().
Or use http_build_query with the $arg_separator argument.
the output of the above is
numeric_prefix: if the array indexes are numbers, this string is added as prefix in each index. In this case the ‘test_0=bar’.
arg_separator : is used to separate arguments. If non is given php use the arg_separator.output difined in php.ini
See php http_build_query
PHP 5.3.1 (Buggy Behavior)
http_build_query DOES escape the ‘&’ ampersand character that joins the parameters. Example:
user_id=1&setting_id=2
.PHP 5.4+
http_build_query DOES NOT escape the ‘&’ ampersand character that joins the parameters. Example:
user_id=1&setting_id=2
Example:
Solution when targeting multiple version
Option #1: Write a wrapper function:
Option #2: Ditch the http_build_query function and write your own.
I tried do this recently https://gist.github.com/marcocesarato/4293986fe93139f04d8269aa3fbd47e9 the difference is that this function is array recursive and compatible with PHP4. You can manage here the urlencode
I was trying to create a GET string to attach it to the end of a url like this –
which prints out
But my api didn’t like the numbers in the square brackets, so I found a regular expression that removes the numbers –
The final code, and the result –
Prints out –
include[]=enrollments&include[]=current_grading_period_scores&enrollment_type[]=student
If someone knows of a better regex let me know, I’m kind of a noob with it.