套接字上下文选项
套接字上下文选项 — 套接字上下文选项列表
说明
套接字上下文选项可用于所有工作在套接字上的封装协议,像
tcp
, http
和
ftp
。
更新日志
版本 | 说明 |
---|---|
7.1.0 |
添加 tcp_nodelay 。
|
7.0.1 |
添加 ipv6_v6only 。
|
范例
示例 #1 基础的 bindto
用法示例
<?php
// 使用 IP '192.168.0.100' 连接到互联网
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:0',
),
);
// 使用 IP '192.168.0.100' 和端口 '7000' 连接到互联网
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:7000',
),
);
// 使用 IPv6 地址 '2001:db8::1' 和端口 '7000' 连接到互联网
$opts = array(
'socket' => array(
'bindto' => '[2001:db8::1]:7000',
),
);
// 使用端口 '7000' 连接到互联网
$opts = array(
'socket' => array(
'bindto' => '0:7000',
),
);
// 创建上下文...
$context = stream_context_create($opts);
// ...并使用它来读取数据
echo file_get_contents('http://www.example.com', false, $context);
?>

User Contributed Notes 2 notes
guru at jnt-finland dot fi ¶
6 years ago
You can set "bindto" to "0:0" to force use IPv4 instead of IPv6. And probably "[0]:0" to force use IPv6, thou this I couldn't test.