0

我正在使用来自 https://github.com/SparkPost/php-sparkpost#send-an-email-using-the-transmissions-endpoint的示例

这里有异步承诺: https ://github.com/SparkPost/php-sparkpost#then-asynchronous

一切都使用 Composer 正确安装。如果我使用 $response = $promise->wait(); 电子邮件已发送但未发送 $promise->then(function(){}, function(){})

我正在从命令行运行 php 脚本,异步选项设置为 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";
    }
);
4

1 回答 1

1

SparkPost 文档中有一个错误(或只是一个错误的假设)。

->wait()无论如何,你必须以某种方式打电话。因此,只需$promise->wait();在第二个脚本的末尾添加,就可以了。

“不知何故”是指您可以使用all(),some()和其他功能将承诺组合在一起。

于 2018-03-12T14:07:09.710 回答