Http_build_query () without url encoding

Is there a way to use http_build_query() without URL encoding according to some RFC standard?

Why I donโ€™t want the URL to encode everything: I am accessing the Ebay API. They honestly insist that parameter names are not URL encoded, since commas are indicated in brackets. For example. DomainName (0) is a parameter, and the request fails if these parsers are encoded.

+43
php urlencode
May 26 '11 at 1:14
source share
8 answers

You can use urldecode() function in the result line that you get from http_build_query()

+96
Dec 23 '11 at 12:55
source

No, it seems he always wants to encode (which should be, it is intended to encode the URL when creating the list of parameters for the URL).

You can make your own ...

 $params = array('a' => 'A', 'b' => 'B'); $paramsJoined = array(); foreach($params as $param => $value) { $paramsJoined[] = "$param=$value"; } $query = implode('&', $paramsJoined); 

CodePad .

+5
May 26 '11 at 1:19
source

Instead, you can try using the JSON API. I tried to get a working sample, but I do not have the application name, so I can not check the result. Here is the code:

 <?php $appName = "Your App Name Here"; $post_data = array( 'jsonns.xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'jsonns.xs' => 'http://www.w3.org/2001/XMLSchema', 'jsonns.tns' => 'http://www.ebay.com/marketplace/search/v1/services', 'tns.findItemsByKeywordsRequest' => array( 'keywords' => 'harry potter pheonix' ) ); $headers = array( "X-EBAY-SOA-REQUEST-DATA-FORMAT: JSON", "X-EBAY-SOA-RESPONSE-DATA-FORMAT: JSON", "X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords", "X-EBAY-SOA-SECURITY-APPNAME: $appName" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://svcs.ebay.com/services/search/FindingService/v1'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); if($result) { $response = json_decode($result); } curl_close($ch); ?> 

You need to populate $appName any application name. You also need to set X-EBAY-SOA-OPERATION-NAME to the actual call, and change the JSON if the call is different.

+4
May 26 '11 at 1:58
source

http_build_query () is INVALID without urlencoding. Maybe you accidentally encode it twice? For example, try creating an HTTP request for this array:

 $params = array('a' => 'a&b=b'); 

Without coding you will receive

 a=a&b=b 

With the right coding, you will get

 a=a%26b%3Db 

what is right. If you miss the encoding, be sure to avoid attacks using URL attacks.

+4
Apr 04 '12 at 9:18
source

PHP 5.3.1 (Buggy Behavior) http_build_query Lets exit the ampersand '&' mode, which combines the parameters. Example: user_id=1&amp;setting_id=2 .

PHP 5.4 + http_build_query does NOT execute the '&' ampersand, which combines parameters. Example: user_id=1&setting_id=2

Example:

 $params = array( 'user_id' => '1', 'setting_id' => '2' ); echo http_build_query($params); // Output for PHP 5.3.1: user_id=1&amp;setting_id=2 // NOTICE THAT: '&' character is escaped // Output for PHP 5.4.0+: user_id=1&setting_id=2 // NOTICE THAT: '&' character is NOT escaped 

Solution when targeting multiple versions

Option # 1: Write the wrapper function:

 /** * This will work consistently and will never escape the '&' character */ function buildQuery($params) { return http_build_query($params, '', '&'); } 

Option # 2: Disable the http_build_query function and write your own.

+4
Feb 18 '17 at 16:25
source

You can use urldecode () . Or use http_build_query with the $ arg_separator argument.

  $query_data= $data = array('bar', 'baz'=>'boom'); $numeric_prefix= 'test_'; $arg_separator = '&'; $http_query = http_build_query ( $query_data, $numeric_prefix, $arg_separator ); var_dump( $http_query ); 

output above:

  string 'test_0=bar&baz=boom' (length=19) 

numeric_prefix: if the array indices are numbers, this row is added as a prefix in each index. In this case, "test_0 = bar".

arg_separator: used to separate arguments. If non specified by php, use arg_separator.output spilled in php.ini

See php http_build_query

+1
Aug 25 '15 at 18:34
source

my answer is alex but faster

 $params = array('a' => 'A', 'b' => 'B'); $query = ''; foreach ($params as $param => $value) { $query .= $param.'='.$value .'&'; } echo substr($query, 0, -1); 
0
Sep 27 '11 at 16:26
source

It may also work.

 $fields = array( 'a' => 'A', 'b' => 'B' ); $separator = ''; foreach($fields as $key=>$value) { $fields_string .= $separator . $key . '=' . $value; $separator = '&'; } 
0
Aug 31 '12 at 19:07
source



All Articles