Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP curl not returning data in all cases

Tags:

php

curl

imap

I am implementing a basic IMAP client that supports multiple connection libraries - php_imap, sockets and curl.

The php_imap and sockets methods work fine. The curl method works for all commands except FETCH so I know I don't have an error in my code. If I send the same command via sockets it works, so I also know that I don't have an error in my command. Additionally if I enable verbose output I can see that the message is actually sent back.

    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;
    }

This works:

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

This returns empty string (UID is valid - works with socket)

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

Is there is bug in the curl IMAP implementation? I've tested with curl 7.30.0 and 7.35.0

like image 345
Wayne Allen Avatar asked Apr 30 '26 19:04

Wayne Allen


1 Answers

If you need response in $res

$res = curl_exec($ch);

You must set

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Otherwise curl sends it to stdout

like image 178
savvot Avatar answered May 03 '26 09:05

savvot