stream_context_create解析

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

stream_context_create — 创建资源流上下文

说明 ¶

stream_context_create ([ array  $options [, array  $params ]] ) : resource

创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值。

参数 ¶

options

必须是一个二维关联数组,格式如下:$arr['wrapper']['option'] = $value 。

默认是一个空数组。

params

必须是 $arr['parameter'] = $value 格式的关联数组。 请参考 context parameters 里的标准资源流参数列表。

返回值 ¶

上下文资源流,类型为 resource 。

实例:PHP:stream_context_create函数模拟POST/GET请求

<?php
$data = array(
	'name'   => 'zhangsan',
	'gender' => 'male',
	'age'	 => 25
	);
$query_string = http_build_query($data);
 
$option = array(
	'http' => array(
		'method' => 'POST',
		'header' => array(
			"Content-type:application/x-www-form-urlencoded",
			"Contnet-length:".strlen($query_string)
			),
		'content'=> $query_string
		)
	);
 
$context = stream_context_create($option);
 
$url = 'http://localhost/test.php';
$content = file_get_contents($url,false,$context);
 
echo $content;

  

test.php文件:

<?php
print_r($_POST);

  请求返回的结果:

Array ( [name] => zhangsan [gender] => male [age] => 25 )

  

注意:method中的方法名称必须是大写!

  

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值