I'm stuck on this problem and can't figure out how to solve it.
I'm using CakePhp 2.x, as a framework, and Mailjet to send mails, and get their status. (Mailjet is a French competitor of Mailchimp). I want to display in my index all the campaigns sent through Mailjet.
In my EmailController :
public function index() {
$clubCampaigns = $this->getMJAllCampaigns('emailofthe@club.com');
$viewCampaigns = array();
foreach ($clubCampaigns as $key => $Campaign) {
$viewCampaigns[$key]['Subject'] = $Campaign->Subject;
$viewCampaigns[$key]['Campaign'] = $this->getMJCampaign($Campaign->ID);
$viewCampaigns[$key]['Messages'] = $this->getMJMessageSentStatistics($Campaign->ID);
}
$this->set('viewCampaigns', $viewCampaigns);
return $this->render();
}
I get my email campaign with this function.
protected function getMJAllCampaigns($senderID = null, $fromTS = null, $toTS = null ) {
/* @render none */
$this->autoRender = false;
if($senderID == null || $senderID == ''){
throw new Exception('$SenderID is NULL : Cannot request Mailjet API');
return false;
}
if($toTS == null){
$toTS = new DateTime();
}
if($fromTS == null){
$fromTS = new DateTime();
$fromTS->sub(new DateInterval("P1W"));
}
/* Create Mailjet Object */
$Mailjet = new Mailjet();
$params = array (
'Limit' => 1000,
'FromID' => $SenderID,
'From' => $SenderID,
'FromTS' => $fromTS->format(DATE_RFC3339), //Format T RFC 3339 DateTime
'ToTS' => $toTS->format(DATE_RFC3339) //Format T RFC 3339 DateTime
);
$MJCampaign = $Mailjet->campaign($params);
/* Parsing cURL response */
$countCampaign = $MJCampaign->Count;
$campaigns = $MJCampaign->Data;
if($countCampaign == 0 || $campaigns == null){
//throw new Exception('No campaign found - Break');
$campaigns = false;
}
return $campaigns;
}
I'm using a Mailjet Object, given by the Mailjet API. This API is a bench of PHP functions creating cURL resquest.
I know this code is, in state, functional.
Because when I var_dump($clubCampaigns); die;
, it renders the expected array.
But when I try to launch it "naturally", the view is not rendered, loading the time set as max_execution_time
in my php.ini
...
I think that CakePhp is trying to execute the foreach
and render the view before the $this->getMJAllCampaigns
is complete.
So my question is :
How can I tell my foreach to start only when my $this->getMJAllCampaigns()
is completely done ?
Thanks for your time.