Welcome Guest, Not a member yet? Register   Sign In
Porting MailChimp to CI
#1

[eluser]jpcody[/eluser]
Hey folks, I'm looking to port a working MailChimp script to CI; not looking to do much fancy here, and I understood from the docs what needed to happen was creating a new library in system/libraries, where I would place the MailChimp API wrapper, then place my call in a new method. But it appears my library has gotten borked. There are apparently no syntax errors, but I'm getting a white screen of death. Here is my controller code:

Code:
<?php

class Mail extends Controller {

    function storeAddress(){
        $data['email'] = $this->input->post('email');
        $data['api'] = '[apikeyhere];
        $data['list_id'] = '[listidhere]';
        $this->load->library('Mcapi',$data);
        
        // Validation
        if(!$data['email']){ return "No email address provided"; }
    
        if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $data['email'])) {
            return "Email address is invalid";
        }
            
        if($this->mcapi->listSubscribe($data['list_id'], $data['email'], '') === true) {
            // It worked!    
            return 'Success! Check your email to confirm sign up.';
        }else{
            // An error ocurred, return error message    
            // return 'Error: ' . $api->errorMessage;
            return 'Error: Doh';
        }
        
    }

}

In the actual library, I can echo out values all the way until the output buffer begins. Then things seem to stop working. Any ideas where I'm going wrong?

The code is too long to paste here, so I've placed it at http://droplr.com/12Fd5u

Thanks so much for any pointers you could give on implementing this. Mega bonus points if you can help me learn whatever I'm screwing up.
#2

[eluser]Buso[/eluser]
you can't output with return, you have to use $this->load->view() or parser->parse or echo, printf, print_r, exit, die, etc
#3

[eluser]jpcody[/eluser]
Wow, it was a combination of that and me using an address that was rejected by MailChimp. If it's not too dumb of a question, is this simply a feature of CodeIgniter that disable using return in this way? I'm certainly a novice, but would love to make sure how to avoid this in the future.
#4

[eluser]MT206[/eluser]
As far as I know, 'return' never prints to the screen. As Buso said you would have to use something like echo or print to output the data to the screen and in the case of Codeigniter you would probably want to have your error messages display in a view and you could use a flash data(check the sessions section of the Codeigniter user guide) like:

Code:
//set your message
$this->session->set_flashdata('invalid_email', 'Email address is invalid');
//display your message
$this->session->flashdata('invalid_email');
#5

[eluser]jpcody[/eluser]
Awesome, I will make these updates! Thanks for the pointers, MT206.




Theme © iAndrew 2016 - Forum software by © MyBB