在 Laravel 中使用 Mailchimp 自动导出订阅者

Automatically export subscribers using Mailchimp in Laravel

我正在创建一个管理区域,作为跟踪订阅者的简便方法。我只想在每次有人新订阅我的列表时显示和更新他们的电子邮件地址。如下所示,我希望电子邮件地址位于电子邮件下方。我确信我需要使用某种代码来获取订户信息并显示它,但我不确定从哪里获得它。我正在使用 laravel 包 https://github.com/skovmand/mailchimp-laravel

这是我的 mailchimp 控制器:

<?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');
        }
    }
}

这是我的 admin.blade.php 文件,我希望显示电子邮件地址。

@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

如果还有其他需要,请告诉我。如有任何帮助,我们将不胜感激!

为此,您需要访问 MailChimp API。这是一项 REST 服务,因此您可以使用 php-curl.

进行访问

这是一个例子:

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;
}