1

I am making an admin area as an easy way to keep track of my subscribers. All I want is to display and update their email address every time someone new subscribes to my list. As shown below I want the email addresses to go under email. I am sure there is some sort of code that I need to use to get subscriber information and display it, but I am not sure where to get it. I am using the laravel package https://github.com/skovmand/mailchimp-laravel

enter image description here

Here is my mailchimp conbtroller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class MailChimpController extends Controller
{
    public $mailchimp;
    public $listId = '111111111';

    public function __construct(\Mailchimp $mailchimp)
    {
        $this->mailchimp = $mailchimp;
    }

    public function manageMailChimp()
    {
        return view('mailchimp');
    }

    public function subscribe(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
        ]);

        try {


            $this->mailchimp
            ->lists
            ->subscribe(
                $this->listId,
                ['email' => $request->input('email')]
            );

            return redirect()->back()->with('success','Email Subscribed successfully');

        } catch (\Mailchimp_List_AlreadySubscribed $e) {
            return redirect()->back()->with('error','Email is Already Subscribed');
        } catch (\Mailchimp_Error $e) {
            return redirect()->back()->with('error','Error from MailChimp');
        }
    }
}

and here is my admin.blade.php file I want the email addresses to be displayed.

@extends('layouts.app')

@section('content')
<div class="container">
    <h1 class="text-center">Subscribers</h1>
    <table class="table">
        <thead>
            <tr>
                <th>Check</th>
                <th>Email</th>
                <th>City</th>
                <th>Airport</th>
            </tr>
            <tbody>
                <tr>
                    <td></td>
                </tr>
            </tbody>
        </thead>

    </table>
</div>
@endsection

Let me know if there is anything else I need. Any help would be appreciated!

avasssso
  • 257
  • 2
  • 10
  • 22

1 Answers1

1

To do this, you need to access the MailChimp API. It's a REST service, so you can access using php-curl.

Here's an example:

public function getMailChimpList() {
    $mailchimpId = "YOUR_MAILCHIMP_ID";
    $listId = "LIST_ID";
    $apiKey = "YOUR_API_KEY";
    $mailchimpUrl = "https://us11.api.mailchimp.com/3.0";

    //this url may be different, check your API endpoints
    $url = $mailchimpUrl . '/lists//' . $listId . '/members/';
    $header = 'Authorization: apikey ' . $apiKey;

    return self::sendData($url, $header);
}

private static function sendData($url, $header) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($header, 'Content-Type: application/json'));
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $result = trim(curl_exec($curl));
    curl_close($curl);
    return $result;
}
  • Would this go in my controller? – avasssso Jun 27 '16 at 16:02
  • Yes, you cant add it to your controller and add the function "getMailchimpList()" to your routes, so you can access from browser. – Everton Mendonça Jun 27 '16 at 16:05
  • If you need to do other operations in your list, I recommend reading the mailchimp API reference: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/# – Everton Mendonça Jun 27 '16 at 16:07
  • I also seem to be getting an error with this bit $url = $mailchimpUrl . '/lists//' . $listId . '/members/'; $header = 'Authorization: apikey ' . apiKey; Use of undefined constant apiKey - assumed 'apiKey' – avasssso Jun 27 '16 at 16:22
  • Ops, my mistake. The correct line is: $header = 'Authorization: apikey ' . $apiKey; – Everton Mendonça Jun 27 '16 at 16:27
  • hmm now I am getting an error on this line return sendData($postdata, $url, $header); Call to undefined function App\Http\Controllers\sendData() – avasssso Jun 27 '16 at 16:34
  • I'd recommend using [HTTP Basic Auth](http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) instead of that authentication method. Basic Auth isn't MailChimp specific and will make your code easier to follow. – TooMuchPete Jun 27 '16 at 21:02