PHP完全自学手册(珍藏版) 中文pdf扫描版下载
一、代码
1、index.php
$data1 = array('foo','bar','baz','boom','cow'=>'milk','php'=>'hypertext processor'); $data = http_build_query($data1); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $data //'timeout' => 60 * 60 // 超时时间(单位:s) ) ); $url = "http://127.0.0.1/test1/test2.php"; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;
2、test2.php
$data = $_POST; echo '<pre>'; print_r( $data ); echo '</pre>';
3、执行index.php文件结果
Array ( [0] => foo [1] => bar [2] => baz [3] => boom [cow] => milk [php] => hypertext processor )
二、http_build_query分析
(PHP 5, PHP 7)http_build_query($query_data) — 生成 URL-encode 之后的请求字符串
参数$query_data只能是数组或者对象,不能是字符串,如果$query_data是一个对象只有public的属性会加入结果
三、http_build_query代码实例,使用数字下标的元素
1、代码
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor'); echo http_build_query($data) . "\n"; echo http_build_query($data, 'myvar_');
2、结果,以上例程会输出:
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