zoukankan      html  css  js  c++  java
  • 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中的方法名称必须是大写!
    

      

  • 相关阅读:
    Collections集合工具类排序
    集合的学习
    gitee使用方法
    vue 首屏优化
    vue 配置多个路由别名
    vue中的状态管理Vuex
    【Python】Pandas合并表格之(append, join , concat方法)
    elementui中提交表单自动刷新页面的问题
    滴滴实习面试题
    CSS 日常积累
  • 原文地址:https://www.cnblogs.com/lxwphp/p/11377592.html
Copyright © 2011-2022 走看看