未调用PHP流通知回调
我一直在尝试使用PHP流,并开始编写这里显示的类。至少可以说,PHP文档在这方面有点贫乏 我很难让我的流上下文调用指定的回调方法。如果我使用像未调用PHP流通知回调,php,networking,sockets,callback,stream,Php,Networking,Sockets,Callback,Stream,我一直在尝试使用PHP流,并开始编写这里显示的类。至少可以说,PHP文档在这方面有点贫乏 我很难让我的流上下文调用指定的回调方法。如果我使用像file\u get\u contents或fopen这样的函数连接到套接字,则会调用回调,但如果我使用stream\u socket\u client则不会 我认为应该是这样,因为我正在将上下文传递给stream\u socket\u client,如果我使用stream\u socket\u recvfrom我会从套接字返回与fgets返回相同的字符串
file\u get\u contents
或fopen
这样的函数连接到套接字,则会调用回调,但如果我使用stream\u socket\u client
则不会
我认为应该是这样,因为我正在将上下文传递给stream\u socket\u client
,如果我使用stream\u socket\u 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);
?>
我也发现了这个有点相关的PHP bug报告,但看起来报告毫无意义:
从php 5.3.0开始,套接字流似乎不支持这一点 我能找到的唯一调用通知函数(在C代码中)的函数是main/streams/streams.C中的php_stream_notification_notify。还有一些#定义 这可以归结为对php\u stream\u notification\u notify的调用。ftp包装,例如调用
php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
在php_ftp_fopen_connect中。curl和http包装器也是如此。但是对于stream_socket_client()或相关函数没有这样的调用。如果您使用tcp:(甚至文件:)之类的传输替换协议包装器,那么上的示例将不起作用。谢谢您提供的信息。您认为stream_socket_client()触发回调有意义吗?也许我会提交一份bug报告。我对通知程序了解不够,无法给你一个明确的答案。也许它们(出于某种原因)仅用于协议包装器。但至少php_stream_notify_info和php_stream_notify_error会很好。
php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);