phpguzzlesparkpost

SparkPost client library for PHP: asynch promise.then() send not sending


I'm using the example from https://github.com/SparkPost/php-sparkpost#send-an-email-using-the-transmissions-endpoint

with the asynch promise here: https://github.com/SparkPost/php-sparkpost#then-asynchronous

Everything is installed properly using Composer. If I use $response = $promise->wait(); email is sent but not $promise->then(function(){}, function(){})

I'm running php script from command line, asynch option set to true

/// this works:

try {
    $response = $promise->wait();
    echo $response->getStatusCode()."\n";
    print_r($response->getBody())."\n";
} catch (\Exception $e) {
   echo $e->getCode()."\n";
   echo $e->getMessage()."\n";
}


// but this doesn't 
$promise->then(
    // Success callback
    function ($response) {
        echo $response->getStatusCode()."\n";
        print_r($response->getBody())."\n";
    },
    // Failure callback
    function (Exception $e) {

        echo $e->getCode()."\n";
        echo $e->getMessage()."\n";
    }
);

Solution

  • There is a mistake (or just an bad assumption) in the SparkPost docs.

    You have to call ->wait() somehow anyway. So just add $promise->wait(); in the end of your second script, and it'll be fine.

    By "somehow" I mean that you can combine promises together using all(), some() and other functions.