【问题标题】:PHP Stream Notification Callback Not Invoked未调用 PHP 流通知回调
【发布时间】:2010-11-22 04:02:30
【问题描述】:

我一直在使用 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 networking sockets callback stream


    【解决方案1】:

    从 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 的示例将不起作用。

    【讨论】:

    • 感谢您的信息。您认为 stream_socket_client() 触发回调有意义吗?也许我会提交一份错误报告。
    • 我对通知器的了解还不够,无法给你一个明确的答案。也许它们(出于某种原因)仅用于协议包装器。但至少 php_stream_notify_info 和 php_stream_notify_error 会很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多