未调用 PHP 流通知回调

标签 php networking sockets callback stream

我一直在玩弄 PHP Streams,并通过开始编写此处显示的类进行了试验。至少可以说,PHP 文档在这方面有点精简。

我很难让我的流上下文调用指定的回调方法。如果我使用 file_get_contentsfopen 之类的函数连接到套接字,则会调用回调,但如果我使用 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 错误报告,但看起来该报告毫无意义:

http://bugs.php.net/bug.php?id=42387&edit=1

最佳答案

从 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/

相关文章:

javascript - Bootstrap data-id 不传递值

php - node.js 与 Apache PHP 一起运行?

networking - SSH 监听器未关闭

c - 如何正确使用c中的线程和套接字?

python - 在 python 3.5 上打开 UDP 套接字

PHP 不适用于 MAMP?

php - 我可以使用提交按钮来激活 AJAX 功能吗?

c# - StorageException : Blob data corrupted. 收到的字节数不正确 - 这是我的代码中的错误还是 azure 服务器问题?

java - Android 库的套接字总是超时

sockets - 改善PyMongo套接字接收