dongqian1925 2015-11-06 08:54
浏览 13
已采纳

如何在(Mailjet)cURL请求完成时呈现视图?

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.

  • 写回答

1条回答 默认 最新

  • dsbgltg159136540 2015-11-07 17:04
    关注

    You could try this to get every campaigns from a specific sender:

    $result = $mj->campaign([
        'FromEmail' => 'sender email'
    ]);
    
    var_dump($result);
    

    See this documentation: http://dev.mailjet.com/email-api/v3/campaign/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行