Params with null value do not present in result string.
<?
$arr = array('test' => null, 'test2' => 1);
echo http_build_query($arr);
?>
will produce:
test2=1
http_build_query
(PHP 5)
http_build_query — URL エンコードされたクエリ文字列を生成する
説明
$query_data
[, string $numeric_prefix
[, string $arg_separator
[, int $enc_type = PHP_QUERY_RFC1738
]]] )与えられた連想配列 (もしくは添字配列) から URL エンコードされたクエリ文字列を生成します。
パラメータ
-
query_data -
プロパティを含む配列もしくはオブジェクト。
query_dataが配列の場合の形式は、 単純な一次元構造か あるいは配列の配列 (言い換えると、他の配列を含む配列) となります。query_dataがオブジェクトの場合、 public プロパティだけが結果に含められます。 -
numeric_prefix -
もし数値インデックスが基底となる配列に使用されたり
numeric_prefixが指定された場合、 基底となる配列の要素に対する数値インデックスの前にこれが追加されます。これは、後で PHP や他の CGI アプリケーションによってデータがデコードされる際、 正当な変数名になるよう考慮したものです。
-
arg_separator -
arg_separator.output が区分のためのセパレータとして使用されます。ただし、 このパラメータが指定されていた場合は それが使用されます。
-
enc_type -
デフォルトは
PHP_QUERY_RFC1738です。enc_typeがPHP_QUERY_RFC1738の場合は » RFC 1738 に従ってエンコードされ、メディアタイプは application/x-www-form-urlencoded となります。 つまり、スペースはプラス記号 (+) にエンコードされるということです。enc_typeがPHP_QUERY_RFC3986の場合は » RFC 3986 に従ってエンコードされ、 スペースはパーセント形式 (%20) となります。
返り値
URL エンコードされた文字列を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.4.0 |
enc_type パラメータが追加されました。
|
| 5.1.3 | 角括弧がエスケープされるようになりました。 |
| 5.1.2 |
パラメータ arg_separator が追加されました。
|
例
例1 http_build_query() の簡単な使用法
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
上の例の出力は以下となります。
foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor
例2 数値インデックス要素の場合での http_build_query()
<?php
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, 'myvar_');
?>
上の例の出力は以下となります。
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
例3 複雑な配列の場合での http_build_query()
<?php
$data = array('user'=>array('name'=>'Bob Smith',
'age'=>47,
'sex'=>'M',
'dob'=>'5/12/1956'),
'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
'children'=>array('bobby'=>array('age'=>12,
'sex'=>'M'),
'sally'=>array('age'=>8,
'sex'=>'F')),
'CEO');
echo http_build_query($data, 'flags_');
?>
この例は以下を出力します: (可読性のため適宜折り返しています)
user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M& user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera& pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12& children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8& children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO
注意:
基底の配列内の数値インデックス要素 "CEO" のみ、 接頭辞を受け取ります。pastimes 以下にある他の数値インデックスは、 正当な変数名にするための文字列の接頭辞を要求しません。
例4 オブジェクトの場合の http_build_query() の使用
<?php
class parentClass {
public $pub = 'publicParent';
protected $prot = 'protectedParent';
private $priv = 'privateParent';
public $pub_bar = Null;
protected $prot_bar = Null;
private $priv_bar = Null;
public function __construct(){
$this->pub_bar = new childClass();
$this->prot_bar = new childClass();
$this->priv_bar = new childClass();
}
}
class childClass {
public $pub = 'publicChild';
protected $prot = 'protectedChild';
private $priv = 'privateChild';
}
$parent = new parentClass();
echo http_build_query($parent);
?>
上の例の出力は以下となります。
pub=publicParent&pub_bar%5Bpub%5D=publicChild
参考
- parse_str() - 文字列を処理し、変数に代入する
- parse_url() - URL を解釈し、その構成要素を返す
- urlencode() - 文字列を URL エンコードする
- array_walk() - 配列の全ての要素にユーザー関数を適用する
When using the http_build_query function to create a URL query from an array for use in something like curl_setopt($ch, CURLOPT_POSTFIELDS, $post_url), be careful about the url encoding.
In my case, I simply wanted to pass on the received $_POST data to a CURL's POST data, which requires it to be in the URL format. If something like a space [ ] goes into the http_build_query, it comes out as a +. If you're then sending this off for POST again, you won't get the expected result. This is good for GET but not POST.
Instead you can make your own simple function if you simply want to pass along the data:
<?php
$post_url = '';
foreach ($_POST AS $key=>$value)
$post_url .= $key.'='.$value.'&';
$post_url = rtrim($post_url, '&');
?>
You can then use this to pass along POST data in CURL.
<?php
$ch = curl_init($some_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_url);
curl_exec($ch);
?>
Note that at the final page that processes the POST data, you should be properly filtering/escaping it.
Is it worth noting that if query_data is an associative array and a value is itself an empty array, or an array of nothing but empty array (or arrays containing only empty arrays etc.), the corresponding key will not appear in the resulting query string?
E.g.
$post_data = array('name'=>'miller', 'address'=>array('address_lines'=>array()), 'age'=>23);
echo http_build_query($post_data);
will print
name=miller&age=23
As noted before, with php5.3 the separator is & on some servers it seems. Normally if posting to another php5.3 machine this will not be a problem.
But if you post to a tomcat java server or something else the & might not be handled properly.
To overcome this specify:
http_build_query($array, '', '&');
and NOT
http_build_query($array); //gives & to some servers
I noticed that even with the magic quotes disabled, http_build_query() automagically adds slashes to strings.
So, I had to add "stripslashes" to every string variable.
This function is wrong for http!
arrays in http is like this:
files[]=1&files[]=2&...
but function makes like this
files[0]=1&files[1]=2&...
Here is normal function:
<?php
function cr_post($a,$b=\'\',$c=0){
if (!is_array($a)) return false;
foreach ((array)$a as $k=>$v){
if ($c) $k=$b.\"[]\"; elseif (is_int($k)) $k=$b.$k;
if (is_array($v)||is_object($v)) {$r[]=cr_post($v,$k,1);continue;}
$r[]=urlencode($k).\"=\".urlencode($v);}return implode(\"&\",$r);}
?>
on my install of PHP 5.3, http_build_query() seems to use & as the default separator. Kind of interesting when combined with stream_context_create() for a POST request, and getting $_POST['amp;fieldName'] on the receiving end.
Correct implementation of coding the array of params without indexes (valdikks fixed code - didnt work for inner arrays):
<code>
function cr_post($a,$b='',$c=0)
{
if (!is_array($a)) return false;
foreach ((array)$a as $k=>$v)
{
if ($c)
{
if( is_numeric($k) )
$k=$b."[]";
else
$k=$b."[$k]";
}
else
{ if (is_int($k))
$k=$b.$k;
}
if (is_array($v)||is_object($v))
{
$r[]=cr_post($v,$k,1);
continue;
}
$r[]=urlencode($k)."=".urlencode($v);
}
return implode("&",$r);
}
</code>
This function makes like this
files[0]=1&files[1]=2&...
To do it like this:
files[]=1&files[]=2&...
Do this:
$query = http_build_query($query);
$query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
If you need only key+value pairs, you can use this:
<?php
$array = array(
"type" => "welcome",
"message" => "Hello World!"
);
echo urldecode(http_build_query($array, '', ';'));
?>
Result: type=welcome;message=Hello World!
