PHP curl 在所有情况下都不返回数据

标签 php curl imap

我正在实现一个支持多个连接库的基本 IMAP 客户端 - php_imap、套接字和 curl。

php_imap 和套接字方法工作正常。 curl 方法适用于除 FETCH 之外的所有命令,因此我知道我的代码没有错误。如果我通过套接字发送相同的命令,它会起作用,所以我也知道我的命令没有错误。此外,如果我启用详细输出,我可以看到消息实际上已发回。

    function write($command) {
    $url = ($this->type == 'imap' ? 'imap' : 'pop3') . ($this->secure ? 's' : '') . '://' . $this->host . '/INBOX';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PORT, $this->port);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

    $fp = tmpfile();
    curl_setopt($ch, CURLOPT_FILE, $fp);

    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $verbose = fopen('php://temp', 'w+');
    curl_setopt($ch, CURLOPT_STDERR, $verbose);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $command);

    $res = curl_exec($ch);

    rewind($verbose);
    $verboseLog = stream_get_contents($verbose);
    echo($verboseLog);
    fclose($verbose);

    return $res;
    }

这个有效:

write('STATUS "INBOX" (MESSAGES)');

这将返回空字符串(UID 有效 - 适用于套接字)

$uid = 181;
write('UID FETCH ' . $uid . ' (BODY[])');

curl IMAP 实现中是否存在错误?我已经使用 curl 7.30.0 和 7.35.0 进行了测试

最佳答案

如果你需要在 $res 中回复

$res = curl_exec($ch);

你必须设置

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

否则curl将它发送到stdout

关于PHP curl 在所有情况下都不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870022/

相关文章:

php从文本字段到数组

javascript - 如何在codeigniter中调用提交函数

php - 为什么 mysql_insert_id() 返回 0?

php - PHP上的数据显示(选择查询)

linux - bash - 散列变量的二进制内容而不创建文件

python - 如何获取 UTF-8 格式的电子邮件?

php - 使用 Zend 进行 curl http 身份验证

php - cURL 请求 - 我该如何做?

java - 如何显示下载附件的进度条

PHPUnit 电子邮件测试