我一直在玩弄 PHP Streams,并通过开始编写此处显示的类进行了试验。至少可以说,PHP 文档在这方面有点精简。
我很难让我的流上下文调用指定的回调方法。如果我使用 file_get_contents
或 fopen
之类的函数连接到套接字,则会调用回调,但如果我使用 stream_socket_client
,则不会。
我认为它应该是因为我将上下文传递给 stream_socket_client
并且如果我使用 stream_socket_recvfrom
我会从套接字返回与 fgets 返回的字符串相同的字符串。
文章末尾有相关的 PHP 文档链接。
class IMAP {
// Connection Parameters
private $host;
private $port;
private $timeout;
// Credentials
private $email;
private $password;
private $client;
private $transcript;
function __construct($connection, $credentials) {
// Set Connection Settings
$this->host = $connection['host'];
$this->port = $connection['port'];
$this->timeout = $connection['timeout'];
// Set Credentials
$this->email = $credentials['email'];
$this->password = $credentials['password'];
// Connect to the IMAP server
$params = array('notification'=>array($this, 'getLine'));
$ctx = stream_context_create();
stream_context_set_params($ctx, $params);
$this->client = stream_socket_client("tcp://$this->host:$this->port",$errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);
stream_socket_sendto($this->client, "a001 NOOP\r\n");
}
function getLine($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
$args = func_get_args();
var_dump($args);
}
}
$connection = array(
'host' => 'somehost',
'port' => 143,
'timeout' => 10
);
$credentails = array(
'email' => 'someemail',
'password' => 'somepassword'
);
$imap = new IMAP($connection, $credentails);
?>
http://us3.php.net/manual/en/function.stream-context-set-params.php http://us3.php.net/manual/en/context.params.php
我也发现了这个有点相关的 PHP 错误报告,但看起来该报告毫无意义:
最佳答案
从 php 5.3.0 开始,套接字流似乎不支持此功能。
我能找到的调用通知函数(在 C 代码中)的唯一函数是 main/streams/streams.c 中的 php_stream_notification_notify。还有一些#define
#define php_stream_notify_info
#define php_stream_notify_progress
#define php_stream_notify_progress_init
#define php_stream_notify_progress_increment
#define php_stream_notify_file_size
#define php_stream_notify_error
归结为对 php_stream_notification_notify 的调用。 ftp 包装器,例如通话
php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
在 php_ftp_fopen_connect 中。与 curl 和 http 包装器相同。但是没有对 stream_socket_client() 或相关函数的调用。以及 http://php.net/function.stream-notification-callback 处的示例如果将协议(protocol)包装器替换为 tcp:(甚至 file:)之类的传输,则不起作用。
关于未调用 PHP 流通知回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1320420/