1 |
<?PHP |
1 |
<?PHP |
2 |
// +----------------------------------------------------------------------+ |
2 |
// +----------------------------------------------------------------------+ |
3 |
// | PHP Version 4 | |
3 |
// | PHP Version 4 | |
4 |
// +----------------------------------------------------------------------+ |
4 |
// +----------------------------------------------------------------------+ |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
6 |
// +----------------------------------------------------------------------+ |
6 |
// +----------------------------------------------------------------------+ |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
9 |
// | available at through the world-wide-web at | |
9 |
// | available at through the world-wide-web at | |
10 |
// | http://www.php.net/license/3_0.txt. | |
10 |
// | http://www.php.net/license/3_0.txt. | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
14 |
// +----------------------------------------------------------------------+ |
14 |
// +----------------------------------------------------------------------+ |
15 |
// | Authors: Stephan Schmidt <schst@php.net> | |
15 |
// | Authors: Stephan Schmidt <schst@php.net> | |
16 |
// | Aidan Lister <aidan@php.net> | |
16 |
// | Aidan Lister <aidan@php.net> | |
17 |
// +----------------------------------------------------------------------+ |
17 |
// +----------------------------------------------------------------------+ |
18 |
// |
18 |
// |
19 |
// $Id: http_build_query.php,v 1.16 2005/05/31 08:54:57 aidan Exp $ |
19 |
// $Id: http_build_query.php,v 1.16 2005/05/31 08:54:57 aidan Exp $ |
20 |
|
20 |
|
21 |
|
21 |
|
22 |
/** |
22 |
/** |
23 |
* Replace function http_build_query() |
23 |
* Replace function http_build_query() |
24 |
* |
24 |
* |
25 |
* @category PHP |
25 |
* @category PHP |
26 |
* @package PHP_Compat |
26 |
* @package PHP_Compat |
27 |
* @link http://php.net/function.http-build-query |
27 |
* @link http://php.net/function.http-build-query |
28 |
* @author Stephan Schmidt <schst@php.net> |
28 |
* @author Stephan Schmidt <schst@php.net> |
29 |
* @author Aidan Lister <aidan@php.net> |
29 |
* @author Aidan Lister <aidan@php.net> |
30 |
* @version $Revision: 1.16 $ |
30 |
* @version $Revision: 1.16 $ |
31 |
* @since PHP 5 |
31 |
* @since PHP 5 |
32 |
* @require PHP 4.0.0 (user_error) |
32 |
* @require PHP 4.0.0 (user_error) |
33 |
*/ |
33 |
*/ |
34 |
if (!function_exists('http_build_query')) { |
34 |
if (!function_exists('http_build_query')) { |
35 |
function http_build_query($formdata, $numeric_prefix = null) |
35 |
function http_build_query($formdata, $numeric_prefix = null) |
36 |
{ |
36 |
{ |
37 |
// If $formdata is an object, convert it to an array |
37 |
// If $formdata is an object, convert it to an array |
38 |
if (is_object($formdata)) { |
38 |
if (is_object($formdata)) { |
39 |
$formdata = get_object_vars($formdata); |
39 |
$formdata = get_object_vars($formdata); |
40 |
} |
40 |
} |
41 |
|
41 |
|
42 |
// Check we have an array to work with |
42 |
// Check we have an array to work with |
43 |
if (!is_array($formdata)) { |
43 |
if (!is_array($formdata)) { |
44 |
user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.', |
44 |
user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.', |
45 |
E_USER_WARNING); |
45 |
E_USER_WARNING); |
46 |
return false; |
46 |
return false; |
47 |
} |
47 |
} |
48 |
|
48 |
|
49 |
// If the array is empty, return null |
49 |
// If the array is empty, return null |
50 |
if (empty($formdata)) { |
50 |
if (empty($formdata)) { |
51 |
return; |
51 |
return; |
52 |
} |
52 |
} |
53 |
|
53 |
|
54 |
// Argument seperator |
54 |
// Argument seperator |
55 |
$separator = ini_get('arg_separator.output'); |
55 |
$separator = ini_get('arg_separator.output'); |
56 |
|
56 |
|
57 |
// Start building the query |
57 |
// Start building the query |
58 |
$tmp = array (); |
58 |
$tmp = array (); |
59 |
foreach ($formdata as $key => $val) { |
59 |
foreach ($formdata as $key => $val) { |
60 |
if (is_integer($key) && $numeric_prefix != null) { |
60 |
if (is_integer($key) && $numeric_prefix != null) { |
61 |
$key = $numeric_prefix . $key; |
61 |
$key = $numeric_prefix . $key; |
62 |
} |
62 |
} |
63 |
|
63 |
|
64 |
if (is_scalar($val)) { |
64 |
if (is_scalar($val)) { |
65 |
array_push($tmp, urlencode($key).'='.urlencode($val)); |
65 |
array_push($tmp, urlencode($key).'='.urlencode($val)); |
66 |
continue; |
66 |
continue; |
67 |
} |
67 |
} |
68 |
|
68 |
|
69 |
// If the value is an array, recursively parse it |
69 |
// If the value is an array, recursively parse it |
70 |
if (is_array($val)) { |
70 |
if (is_array($val)) { |
71 |
array_push($tmp, __http_build_query($val, urlencode($key))); |
71 |
array_push($tmp, __http_build_query($val, urlencode($key))); |
72 |
continue; |
72 |
continue; |
73 |
} |
73 |
} |
74 |
} |
74 |
} |
75 |
|
75 |
|
76 |
return implode($separator, $tmp); |
76 |
return implode($separator, $tmp); |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
// Helper function |
79 |
// Helper function |
80 |
function __http_build_query ($array, $name) |
80 |
function __http_build_query ($array, $name) |
81 |
{ |
81 |
{ |
82 |
$tmp = array (); |
82 |
$tmp = array (); |
83 |
foreach ($array as $key => $value) { |
83 |
foreach ($array as $key => $value) { |
84 |
if (is_array($value)) { |
84 |
if (is_array($value)) { |
85 |
array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key))); |
85 |
array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key))); |
86 |
} elseif (is_scalar($value)) { |
86 |
} elseif (is_scalar($value)) { |
87 |
array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value))); |
87 |
array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value))); |
88 |
} elseif (is_object($value)) { |
88 |
} elseif (is_object($value)) { |
89 |
array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key))); |
89 |
array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key))); |
90 |
} |
90 |
} |
91 |
} |
91 |
} |
92 |
|
92 |
|
93 |
// Argument seperator |
93 |
// Argument seperator |
94 |
$separator = ini_get('arg_separator.output'); |
94 |
$separator = ini_get('arg_separator.output'); |
95 |
|
95 |
|
96 |
return implode($separator, $tmp); |
96 |
return implode($separator, $tmp); |
97 |
} |
97 |
} |
98 |
} |
98 |
} |
99 |
|
99 |
|
100 |
?> |
100 |
?> |