2017-04-20 70 views
0

所以,我已經開發了下面的代碼:問題通過失敗和拒絕事件尋呼通過mailgun的API返回

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文檔,但我不能,我用我的生命,弄清楚它是什麼談論與此:

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 /,但似乎並不是分頁。我不確定在這裏做什麼,甚至不知道我應該做什麼。

回答

0

我知道這比你希望的要遲一點,但我最近也在用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似乎總是甚至提供了一個「下一步」鏈接時,你已經得到了所有你想要的結果。

在我的項目我收集到的所有結果用得到我最初的記錄後,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; 
} 

希望這有助於!