zoukankan      html  css  js  c++  java
  • php Stream Contexts 小记

    摘自于手册
    上下文是修改或增强流的行为的一组参数和包装器特定选项。 上下文使用stream_context_create()创建,并且可以传递到大多数与文件系统相关的流创建函数(即fopen()file()file_get_contents()get_headers()等)。

    当调用stream_context_create()或稍后使用stream_context_set_option()时,可以指定选项。 在上下文(Context)选项和参数一章中可以找到包装器特定选项的列表。

    可以使用stream_context_set_params()函数为上下文指定参数。

    get_headersfile_get_contents为例:

    1.       get_headers

    场景:需要获取URL的返回状态码,且访问超时的设定

     

    $url = 'http://localhost:8000/test/contexts/get_headers_t.php';
    
    stream_context_set_default(
        array(
            'http' => array(
                'method' => 'GET',
                'timeout' => 1,
                'header' => "Content-type: application/x-www-form-urlencoded",
            )
        )
    );
    
    $headers = get_headers($url);

     

    2.       file_get_contents

    场景:获取文件内容的超时时间设定

     

    $url = 'http://localhost:8000/test/contexts/file_get_contents_t.php';
    $contexts = stream_context_create(
        array(
            'http' => array(
                'method' => 'GET',
                'timeout'=> 1,
                'header' => "Content-type: application/x-www-form-urlencoded"
            )
        )
    );
    $headers = file_get_contents($url, false, $contexts);

     

    以上只是使用HTTP context的一些选项,更多选项参考:上下文(Context)选项和参数

     

     

    php Stream Contexts 小记

  • 相关阅读:
    单源最短路径-邻接表无向网络
    带权邻接表图的最小生成树
    邻接矩阵有向图的广度优先遍历
    邻接表无向图的深度优先遍历
    图基础-创造用于测试的简单图
    用EFCore从mssql数据库生成实体类
    使用EF Core访问SqlServer数据库
    哈夫曼树综合练习
    树形结构例--控制台显示文件夹结构
    Java之设计模式
  • 原文地址:https://www.cnblogs.com/wangyulu/p/6546567.html
Copyright © 2011-2022 走看看