我一直在玩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);
?>
我发现这个有点相关的PHP错误报告,但看起来报告毫无意义:
解决方法:
从php 5.3.0开始,套接字流看起来不支持此功能.
我能找到的唯一一个调用通知函数(在C代码中)的函数是main / streams / streams.c中的php_stream_notification_notify.还有一些#defines
#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()或相关函数的调用.如果用tcp :(甚至文件:)之类的传输替换协议包装器,则http://php.net/function.stream-notification-callback的示例不起作用.
标签:php,stream,networking,sockets,callback
来源: https://codeday.me/bug/20190522/1151674.html