【问题标题】:PHP curl not returning data in all casesPHP curl在所有情况下都不返回数据
【发布时间】:2016-06-16 21:57:40
【问题描述】:

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

php_imap 和 sockets 方法工作正常。 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 测试过

【问题讨论】:

    标签: php curl imap


    【解决方案1】:

    如果您需要 $res 中的响应

    $res = curl_exec($ch);
    

    你必须设置

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

    否则 curl 将其发送到标准输出

    【讨论】:

      【解决方案2】:

      如果您使用 Apache HTTPd,请尝试重新启动它。
      它通过 cURL 请求中的空响应解决了我的问题。

      【讨论】:

        【解决方案3】:

        由于某种原因,FETCH 数据作为标头数据返回,默认情况下通过管道传输到 stderr。您可以通过 CURLOPT_HEADERFUNCTION 回调捕获它。例如,要将其存储到文件中,如下所示:

            $fileHandle = tmpfile();
            curl_setopt($this->ch, CURLOPT_HEADERFUNCTION,
              function ($ch, $str) use ($fileHandle) {
                fwrite($fileHandle, $str);
                return strlen($str);
              }
            );
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-27
          • 2011-09-13
          • 1970-01-01
          • 1970-01-01
          • 2014-02-04
          • 1970-01-01
          相关资源
          最近更新 更多