支持的协议/封装协议列表
PHP 手册

Socket

本节包含支持可工作于套接字的封装协议的上下文选项,如 tcphttpftp

自 PHP 5.1.0 起只支持一个选项,bindto,可用来指定 IP 地址(可以是 IPv4 或 IPv6)和/或 PHP 将用来访问网络的端口号。语法是 ip:port(如果想让系统自动选择可以将 IP 或端口号设为 0)。

Note: 因 FTP 在正常运行时创建两个套接字连接,此时不能在 bindto 选项中指定端口号。因此对于 FTP wrapper 只支持一种语法 ip:0

Example #1 怎样使用 bindto 选项的一些例子

<?php
// connect to the internet using the '192.168.0.100' IP
$opts = array('socket' =>
            array(
'bindto' => '192.168.0.100:0'));


// connect to the internet using the '192.168.0.100' IP and port '7000'
$opts = array('socket' =>
            array(
'bindto' => '192.168.0.100:7000'));


// connect to the internet using port '7000'
$opts = array('socket' =>
            array(
'bindto' => '0:7000'));


// create the context...
$context stream_context_create($opts);

// ...and use it to fetch the data
echo file_get_contents('http://www.example.com'false$context);

?>

支持的协议/封装协议列表
PHP 手册