邮件管理器api返回的故障和拒绝事件的分页问题

时间:2017-04-20 15:14:37

标签: php mailgun

所以,我开发了以下代码:

const MAILGUN_API_MAX_LIMIT = 300; //max in documentation
$mgClient = new Mailgun\Mailgun("<mailgun_key>");
$domain = "<my_domain>";
$resultItems = array();

try {
    $result = null;
    $endTime_Epoch = time();
    $startTime_Epoch = $endTime_Epoch - 1440000; //400 hours
    $queryParams = array(
        'begin'        => $endTime_Epoch,
        'end'          => $startTime_Epoch,
        'limit'        => MAILGUN_API_MAX_LIMIT,
        'ascending'    => 'no',
        'event'        => 'rejected OR failed',
    );
    $request_url = "$domain/events";
    do {
        $result = $mgClient->get($request_url, $queryParams);
        if(!$result->http_response_body) {
            /*...*/
        } else if($result->http_response_code != 200) {
           /*...*/
        } else {

            $resultItems[] = $result->http_response_body->items;
            if(count($result->http_response_body->items) == MAILGUN_API_MAX_LIMIT) {
                $request_url = $result->http_repsonse_body->paging->next;
            }
        }
    } while($result && $result->http_response_body && count($result->http_response_body->items) == MAILGUN_API_MAX_LIMIT);
} catch (Exception $e) {
    echo $e->getMessage()
}

但是,如果限制遭到限制(我可能不会,但是如果发生某些事情导致它发生的事情那么会很糟糕),我很难让它通过下一组请求进行翻页。

我已经尝试过阅读mailgun的api doc,但我不能,因为我的生活,弄清楚它是在谈论这个:

require 'vendor/autoload.php';
use Mailgun\Mailgun;

# Instantiate the client.
$mgClient = new Mailgun('YOUR_API_KEY');
$domain = 'YOUR_DOMAIN_NAME';

$nextPage = 'W3siYSI6IGZhbHNlLC';

# Make the call to the client.
$result = $mgClient->get("$domain/events/$nextPage");

但我无法弄清楚$ nextPage应该是什么。我已经尝试剥离分页的开始 - >接下来所以它最终只是$ domain / events /,但这似乎不是分页。我不确定该怎么做,甚至不应该做我想做的事。

1 个答案:

答案 0 :(得分:1)

我知道这比你希望的要晚一点,但我最近也在用Mailgun和PHP做过一些事情。

因此$nextPage 之后提供的第一个API请求值,因此您在开始时并不需要它。只需提出正常的请求即可

$result = $mailgun->get("$domain/events", $queryString);

// then we can get our 'Next Page' uri provided by the request
$nextURI = $result->http_response_body->paging->next;

// now we can use this to grab our next page of results
$secondPageResults = $mailgun->get($nextURI, $queryString);

值得一提的是,Mailgun似乎始终提供下一个&#39;即使你已经得到了你想要的所有结果,也要链接。

在我的项目中,我在获取初始记录后使用while循环收集了所有结果:

$hasNext = count($items) >= $queryLimit;

while($hasNext) {
    $nextResult = $mailgun->get($lastResult->http_response_body->paging->next, $queryString);
    $nextItems = $nextResult->http_response_body->items;
    $items = array_merge($items, $nextItems);

    $hasNext = count($nextItems) >= $queryLimit;
    $lastResult = $nextResult;
}

希望这有帮助!

相关问题