PHP Context学习系列《十》

Context用于所有的文件系统或数据流封装协议。

1、套接字context

主要是tcp,http,ftp这些基于socket的协议。

新加的bindto参数实例:

<?php

$opt = array(
    'socket' => array(
        'bindto' = > '192.168.1.1:8000',
        ),
    );


$context = stream_context_create($opt);

echo file_get_contexts('www.examples.con', false, $context);
?>

2:http context

args:

method (string) 远程服务器支持的请求方式 如get post。

header (string)会覆盖后面定义的user-agent等。

user_agent (string)

max_redirects (integer) 默认最多的重定向有20次,设置成1次或0次不跟随重定向。

context (string)在header后面要发送的额外数据,通常使用post或者put

protocol_version (float) 默认竟然是1.0. 咱们把他设置为1.1 然后可以支持 分块传输解码 (chunked transfer encoding). 以后好好看这个分块传输解码

一个post请求实例:

<?php

$postdata = http_build_query(
    array(
        'var1' => 'post1',
        'var2 => 'post2'
    )
);

$opts = array('http'=>
    array(
        'method' =>'post',
        'header' =>'Context-type:application/x-www-form-urlencoded',
        'content' => $postdata
    
    )
);


$content = stream_context_create($opts);

file_get_contents('www.examples.com/submit.php', false,$content);
原文地址:https://www.cnblogs.com/-Doraemon/p/4749123.html