The remote_socket argument, in its end (well... after the port), can also contain a "/" followed by a unique identifier. This is especially useful if you want to create multiple persistent connections to the same transport://host:port combo.
Example:
<?php
$socket = stream_socket_client('tcp://mysql.example.com:3306/root', $errorno, $errorstr, $timeout, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
?>
Note that while (p)fsockopen() follows a similar scheme, it doesn't have this particular feature.
stream_socket_client
(PHP 5)
stream_socket_client — インターネットドメインまたは Unix ドメインのソケット接続を開く
説明
$remote_socket
[, int &$errno
[, string &$errstr
[, float $timeout = ini_get("default_socket_timeout")
[, int $flags = STREAM_CLIENT_CONNECT
[, resource $context
]]]]] )
remote_socket で指定された接続先との、
ストリームまたはデータグラム接続を確立します。
作成されるソケットのタイプは、[トランスポート]://[ターゲット]
という形式の URL フォーマットによって指定された
トランスポートによって決定されます。
TCP や UDP といったインターネットドメインのソケット (AF_INET)
には、remote_socket パラメータの
ターゲット の部分は、ホスト名または IP アドレスと、
それに続くコロンで区切られたポート番号から構成されていなければなりません。
Unix ドメインのソケットの場合は、ターゲット
の部分は、ファイルシステムにおけるソケットのファイルを指定しなくては
いけません。
注意:
ストリームはデフォルトではブロックモードで開かれますが、 stream_set_blocking() を使うことで非ブロックモードに 変更することができます。
パラメータ
-
remote_socket -
接続するソケットのアドレス。
-
errno -
接続に失敗した場合にシステムレベルのエラー番号が設定されます。
-
errstr -
接続に失敗した場合にシステムレベルのエラーメッセージが設定されます。
-
timeout -
connect() システムコールがタイムアウトとなるまでの秒数。
注意: このパラメータが適用されるのは、非同期通信を試みていない場合のみです。
注意:
ソケット上のデータの読み書きに関してタイムアウトを設定する必要がある場合は、 stream_set_timeout() を使ってください。 stream_socket_client() に渡される
timeoutは、ソケットの接続時にのみ適用されます。 -
flags -
接続設定フラグの任意の組み合わせを指定できるビットフィールドです。 現在、接続設定フラグとして選択できる値は、
STREAM_CLIENT_CONNECT(デフォルト)、STREAM_CLIENT_ASYNC_CONNECTとSTREAM_CLIENT_PERSISTENTのみです。 -
context -
stream_context_create() で作成した有効なコンテキストリソース。
エラー / 例外
失敗した場合は、errno と
errstr に
システムレベルの connect()
関数の実行時に発生したシステムレベルのエラーを表す値を返します。
もし、errno に返された値が 0
で、かつ、 この関数が FALSE を返した時は、
connect() システムコールの前に何らかのエラーが
発生したことを示しています。これは、多くの場合、ソケットの初期化
に失敗したことで起こります。errno と
errstr パラメータは常に参照渡しされることに
留意してください。
例
例1 stream_socket_client() の例
<?php
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
?>
例2 UDP 接続の使用
ローカルホスト上で動いている UDP サービスの "daytime" (ポート 13) で日時の情報を取得します。
<?php
$fp = stream_socket_client("udp://127.0.0.1:13", $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
fwrite($fp, "\n");
echo fread($fp, 26);
fclose($fp);
}
?>
注意
UDP ソケットは、リモートホストに到達できなくても エラーを発生せず、開いているかような状態になることがありす。 このエラーは、実際にソケットに対して、読み込みや書き込み動作を 行ってみないと判断できません。原因としては、UDP が非接続型の プロトコルであることが挙げられます。つまり、実際にデータを 送受信する段階になるまで、OS の側では接続を確立しようとしないという ことです。
注意: 数値で IPv6 アドレスを指定するときは、 (例 fe80::1) アドレスを角カッコでくくらなくてはなりません。たとえば、 tcp://[fe80::1]:80.
注意:
環境により、Unix ドメインや接続タイムアウトが利用できない場合があります。 有効なトランスポートのリストは、stream_get_transports() で取得できます。組み込むのポートの一覧については、 サポートされるソケットトランスポートのリスト を参照ください。
参考
- stream_socket_server() - インターネットドメインまたは Unix ドメインのサーバーソケットを作成する
- stream_set_blocking() - ストリームのブロックモードを有効にする / 解除する
- stream_set_timeout() - ストリームにタイムアウトを設定する
- stream_select() - select() システムコールと同等の操作を、 ストリームの配列に対して tv_sec と tv_usec で指定されたタイムアウト時間をもって行う
- fgets() - ファイルポインタから 1 行取得する
- fgetss() - ファイルポインタから 1 行取り出し、HTML タグを取り除く
- fwrite() - バイナリセーフなファイル書き込み処理
- fclose() - オープンされたファイルポインタをクローズする
- feof() - ファイルポインタがファイル終端に達しているかどうか調べる
- cURL 関数
stream_socket_client is much easier and faster to use to direct sockets, because you can use directly fwrite / fget / fclose functions, but I find hard to find how to connect to a UNIX domain socket. The URL to use is "udg:///path/to/socket".
For example, to log to the log socket (like syslog), you can use:
<?php
$socket = stream_socket_client('udg:///dev/log',
$errorno,
$errorstr,
$timeout);
fwrite($socket, ...);
?>
For those wanting to use stream_socket_client() to connect to a local UNIX socket who can't find documentation on how to do it, here's a (rough) example:
<?php
$sock = stream_socket_client('unix:///full/path/to/my/socket.sock', $errno, $errstr);
fwrite($sock, 'SOME COMMAND'."\r\n");
echo fread($sock, 4096)."\n";
fclose($sock);
?>
the httpPost() function was almost too long for a user-comment.. I've cut some newlines but to be complete the code snippet misses:
<?php
function randomNonce($len=0) {
$chars = "ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz023456789";
$i=0; $rv='';
if ($len < 1) $len= (6+rand()%10);
while ($i++ < $len) {
$rv.=$chars[rand() % strlen($chars)];
}
return $rv;
}
?>
I came here since fsockopen() does not support any SSL certificate checking in PHP5.
while curl is nice, I use stream_socket_client() to make XML-RPC POST requests via HTTPS and since I have not found any PHP code around that does this, I'll attach an example that also includes HTTP-Digest Auth (eg. trac's WikiRPCInterface2):
<?php
#################################################
# $host: hostname ; eg 'example.org'
# $path: request' eg '/index.php?id=123'
# $data_to_send : data to POST after the HTTP header.
#
# if $opts is an empty array() a standard HTTP to port 80 request is performed.
#
# set auth['type']='basic' to use plain-text auth,
# digest-auth will be handled automatically if $auth['username'] is set and a 401
# status is encountered. - use auth['type']='nodigest' to override.
#
##
function httpPost($host, $path, $data_to_send,
$opts=array('cert'=>"", 'headers'=>0, 'transport' =>'ssl', 'port'=>443),
$auth=array('username'=>"", 'password'=>"", 'type'=>"")
) {
$transport=''; $port=80;
if (!empty($opts['transport'])) $transport=$opts['transport'];
if (!empty($opts['port'])) $port=$opts['port'];
$remote=$transport.'://'.$host.':'.$port;
$context = stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'verify_host', true);
if (!empty($opts['cert'])) {
$result = stream_context_set_option($context, 'ssl', 'cafile', $opts['cert']);
$result = stream_context_set_option($context, 'ssl', 'verify_peer', true);
} else {
$result = stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
}
$fp = stream_socket_client($remote, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
trigger_error('httpPost error: '.$errstr);
return NULL;
}
$req='';
$req.="POST $path HTTP/1.1\r\n";
$req.="Host: $host\r\n";
if ($auth['type']=='basic' && !empty($auth['username'])) {
$req.="Authorization: Basic ";
$req.=base64_encode($auth['username'].':'.$auth['password'])."\r\n";
}
elseif ($auth['type']=='digest' && !empty($auth['username'])) {
$req.='Authorization: Digest ';
foreach ($auth as $k => $v) {
if (empty($k) || empty($v)) continue;
if ($k=='password') continue;
$req.=$k.'="'.$v.'", ';
}
$req.="\r\n";
}
$req.="Content-type: text/xml\r\n";
$req.='Content-length: '. strlen($data_to_send) ."\r\n";
$req.="Connection: close\r\n\r\n";
fputs($fp, $req);
fputs($fp, $data_to_send);
while(!feof($fp)) { $res .= fgets($fp, 128); }
fclose($fp);
if ($auth['type']!='nodigest'
&& !empty($auth['username'])
&& $auth['type']!='digest' # prev. digest AUTH failed.
&& preg_match("/^HTTP\/[0-9\.]* 401 /", $res)) {
if (1 == preg_match("/WWW-Authenticate: Digest ([^\n\r]*)\r\n/Us", $res, $matches)) {
foreach (split(",", $matches[1]) as $i) {
$ii=split("=",trim($i),2);
if (!empty($ii[1]) && !empty($ii[0])) {
$auth[$ii[0]]=preg_replace("/^\"/",'', preg_replace("/\"$/",'', $ii[1]));
}
}
$auth['type']='digest';
$auth['uri']='https://'.$host.$path;
$auth['cnonce']=randomNonce();
$auth['nc']=1;
$a1=md5($auth['username'].':'.$auth['realm'].':'.$auth['password']);
$a2=md5('POST'.':'.$auth['uri']);
$auth['response']=md5($a1.':'
.$auth['nonce'].':'.$auth['nc'].':'
.$auth['cnonce'].':'.$auth['qop'].':'.$a2);
return httpPost($host, $path, $data_to_send, $opts, $auth);
}
}
if (1 != preg_match("/^HTTP\/[0-9\.]* ([0-9]{3}) ([^\r\n]*)/", $res, $matches)) {
trigger_error('httpPost: invalid HTTP reply.');
return NULL;
}
if ($matches[1] != '200') {
trigger_error('httpPost: HTTP error: '.$matches[1].' '.$matches[2]);
return NULL;
}
if (!$opts['headers']) {
$res=preg_replace("/^.*\r\n\r\n/Us",'',$res);
}
return $res;
}
?>
# Some may find it useful to know that your caCert
# must be in pem format, and that PHP seems to like
# your key, cert, and cacert pem's to be concatenated
# in a single file (I suffered various "unknown chain"
# errors, otherwise)
#
# So, (linux users), concat your components as follows:
# (where current working dir is dir where
# cert components are stored)
#
# cat key.pem >certchain.pem
# cat cert.pem >>certchain.pem
# cat cacert.pem >>certchain.pem
#
# Then, the php....
##################################
<?php
$host = 'host.domain.tld';
$port = 1234;
$timeout = 10;
$cert = '/path/to/your/certchain/certchain.pem';
$context = stream_context_create(array('ssl'=>array('local_cert'=> $cert,
)));
if ($fp = stream_socket_client('ssl://'.$host.':'.$port, $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $context)) {
fwrite($fp, "\n");
echo fread($fp,8192);
fclose($fp);
} else {
echo "ERROR: $errno - $errstr<br />\n";
}
?>
