http_build_query() without url encoding
Solution 1:
You can use urldecode()
function on a result string which you get from http_build_query()
Solution 2:
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...
$params = array('a' => 'A', 'b' => 'B');
$paramsJoined = array();
foreach($params as $param => $value) {
$paramsJoined[] = "$param=$value";
}
$query = implode('&', $paramsJoined);
CodePad.
Solution 3:
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:
$params = array(
'user_id' => '1',
'setting_id' => '2'
);
echo http_build_query($params);
// Output for PHP 5.3.1:
user_id=1&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 version
Option #1: Write a wrapper function:
/**
* This will work consistently and will never escape the '&' character
*/
function buildQuery($params) {
return http_build_query($params, '', '&');
}
Option #2: Ditch the http_build_query function and write your own.
Solution 4:
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:
<?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'll need to to fill $appName
with whatever the name of the app is. Also the X-EBAY-SOA-OPERATION-NAME
will need to be set to the actual call, and the JSON modified if the call is different.