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/

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)