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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵