stream_context_create

作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
函数原型:resource stream_context_create ([ array $options [, array $params ]] )
用法
例子一:
复制代码 代码如下:

<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.jb51.net
with additional headers shown above */
$fp = fopen('http://www.jb51.net', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

例子二:
复制代码 代码如下:

<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
?>
You would setup the header this way:
<?php
$opts = array( 'http-->array(
'method'=>"GET",
'header'=>array("Accept-language: en",
"Cookie: foo=bar",
"Custom-Header: value")
)
);
$context = stream_context_create($opts);
?>

例子三:
复制代码 代码如下:

<?php
$opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.jb51.net', false, $context);
echo $data;
?>

例子四:
复制代码 代码如下:

<?php
function do_post_request($url, $postdata, $files = null)
{
$data = "";
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
//Collect Postdata
foreach($postdata as $key => $val)
{
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
}
$data .= "--$boundary\n";
//Collect Filedata
foreach($files as $key => $file)
{
$fileContents = file_get_contents($file['tmp_name']);
$data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary--\n";
}
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
'content' => $data
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
//set data (in this example from post)
//sample data
$postdata = array(
'name' => $_POST['name'],
'age' => $_POST['age'],
'sex' => $_POST['sex']
);
//sample image
$files['image'] = $_FILES['image'];
do_post_request("http://www.jb51.net", $postdata, $files);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
_check_food ; 检查是否吃到食物 snake_move_left: dec dx ; 左移一格 jmp snake_move_check_food ; 检查是否吃到食物 snake_move_up: dec si ; 上移一格 jmp snake_move_check_food在 PHP 中,stream_context_create() 函数用于创建并返回一个用于流上下文的资源。 流上下文用 ; 检查是否吃到食物 snake_move_check_food: cmp dx, food_x ; 如果蛇头坐标与食物于在流操作期间控制流的行为,例如设置 HTTP 请求头、SSL 安全协议、代理服务器等坐标重合 je snake_move_eat_food ; 则吃到食物 cmp si, food_y je snake_move_eat_food dec bx ; 否则继续移动 jmp snake_move_loop snake_move_head: mov [。stream_context_create() 可以创建一个包含多个选项和参数的上下文资源,以便在流操作期si*WIDTH+dx], ' ' ; 将蛇头所在坐标改为空格 add si, snake_direction ; 根据间应用这些选项和参数。 stream_context_create() 的语法如下: ```php resource stream_context_create ([ array方向移动蛇头 cmp si, HEIGHT ; 如果蛇头超出屏幕范围 jge snake_move_exit ; 则游戏结束 cmp dx, WIDTH jge snake_move_exit cmp si, snake_tail_y ; 如果蛇 $options [, array $params ]] ) ``` 其中,$options 参数是一个数组,用于设置上下文的选项,头坐标与蛇尾坐标重合 je snake_move_exit ; 则游戏结束 cmp dx, snake_tail_x$params 参数是一个数组,用于设置上下文的参数。 stream_context_create() 常用于以下场景: - 使用 je snake_move_exit mov [si*WIDTH+dx], 'O' ; 将蛇头移动到新的坐 file_get_contents() 函数从远程服务器获取数据时,可以使用 stream_context_create() 函数设置 HTTP 请求头、代理服务器等标 mov [si*WIDTH+dx+1], 0x70 ; 蛇头颜色为黑底白字 mov snake_tail_y, [si-bx*snake_direction] ; 更新蛇尾坐标 Y mov snake_tail_x, [si选项。 - 在流操作中使用 SSL 安全协议时,可以使用 stream_context_create() 函数设置 SSL 相关参数。 -bx*snake_direction+1] ; 更新蛇尾坐标 X mov [snake_tail_y*WIDTH+snake_tail_x],- 在使用 fsockopen() 函数与远程服务器建立 socket 连接时,可以使用 stream_context_create() 函数设置 socket 相 '*' ; 将蛇尾移到新的坐标 mov [snake_tail_y*WIDTH+snake_tail_x+1], 0x07 ; 蛇尾颜色为黑底白字 jmp snake_move_check_food ; 检查是否吃到食物 snake_move关参数。 总之,stream_context_create() 函数可以方便地控制流的行为,提高流操作的灵活性和可定制性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值