【发布时间】:2010-11-22 04:02:30
【问题描述】:
我一直在使用 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 networking sockets callback stream