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/

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

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!