Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据客户的选择(使用Mailchimp)将潜在客户发送到特定的邮件列表?_Javascript_Php_Html - Fatal编程技术网

Javascript 如何根据客户的选择(使用Mailchimp)将潜在客户发送到特定的邮件列表?

Javascript 如何根据客户的选择(使用Mailchimp)将潜在客户发送到特定的邮件列表?,javascript,php,html,Javascript,Php,Html,我正在尝试将我的新闻稿表单与我的邮件列表链接,但是,我希望根据用户所做的选择,线索进入一个特定的列表 例如,如果他们从下面的HTML代码中选择“我是工人”选项,他们将被订阅到“工人”邮件列表中。现在,如果他们从下面的HTML代码中选择“我是一家企业”选项,他们将被订阅到“企业”邮件列表中 我的HTML代码如下: <form class="form"> <div class="row">

我正在尝试将我的新闻稿表单与我的邮件列表链接,但是,我希望根据用户所做的选择,线索进入一个特定的列表

例如,如果他们从下面的HTML代码中选择“我是工人”选项,他们将被订阅到“工人”邮件列表中。现在,如果他们从下面的HTML代码中选择“我是一家企业”选项,他们将被订阅到“企业”邮件列表中

我的HTML代码如下:

                    <form class="form">
                        <div class="row">
                            <div class="col-md-3 col-sm-12 px-1">
                                <div class="form-group">
                                    <input type="text" class="form-control" placeholder="First Name*">
                                </div>
                            </div>
                            <div class="col-md-4 col-sm-12 px-1">
                                <div class="form-group">
                                    <input type="email" class="form-control" placeholder="Email Address*">
                                </div>
                            </div>
                            <div class="col-md-3 col-sm-12 px-1">
                                <div class="form-group">
                                    <select class="form-control">
                                        <option selected disabled>-- Please Select --</option>
                                        <option>I am a Worker</option>
                                        <option>I am a Business</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-2 col-sm-12 px-1 submit-btn">
                                <button type="submit" class="btn btn-primary newsletter-btn">Notify Me</button>
                            </div>
                        </div>
                    </form>


--请选择--
我是一名工人
我是一个商人
通知我
下面是我正在使用的当前notify me.php文件:

<?php

// Set to "mailchimp" or "file"
$STORE_MODE = "mailchimp";

// MailChimp API Key findable in your Mailchimp's dashboard
$API_KEY =  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XX0";

// MailChimp List ID  findable in your Mailchimp's dashboard
$LIST_ID =  "XXXXXXXXXX";

require('MailChimp.php');

/* ***************************************************** */
// For the part below, no interventions are required
/* ***************************************************** */

if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["email"])) {

    $email = $_POST["email"];

    header('HTTP/1.1 200 OK');
    header('Status: 200 OK');
    header('Content-type: application/json');

    // Checking if the email writing is good
    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

        // The part for the storage in a .txt
        if ($STORE_MODE == "file") {

            // SUCCESS SENDING
            if(@file_put_contents($STORE_FILE, strtolower($email)."\r\n", FILE_APPEND)) {
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => "FileAccessError"
                    ));
            }

        // The part for the storage in Mailchimp
        } elseif ($STORE_MODE == "mailchimp") {

            $MailChimp = new \Drewm\MailChimp($API_KEY);

            $result = $MailChimp->call('lists/subscribe', array(
                        'id'                => $LIST_ID,
                        'email'             => array('email'=>$email),
                        'double_optin'      => false,
                        'update_existing'   => true,
                        'replace_interests' => false,
                        'send_welcome'      => true,
                    ));     

            // SUCCESS SENDING
            if($result["email"] == $email) {        
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => $result["name"]
                    ));
            }
        // ERROR
        } else {
            echo json_encode(array(
                    "status" => "error",
                ));
        }
    // ERROR DURING THE VALIDATION 
    } else {
        echo json_encode(array(
                "status" => "error",
                "type" => "ValidationError"
            ));
    }
} else {
    header('HTTP/1.1 403 Forbidden');
    header('Status: 403 Forbidden');
}

?>
<?php

namespace Drewm;

class MailChimp
{
    private $api_key;
    private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
    private $verify_ssl   = false;

    /**
     * Create a new instance
     * @param string $api_key Your MailChimp API key
     */
    function __construct($api_key)
    {
        $this->api_key = $api_key;
        list(, $datacentre) = explode('-', $this->api_key);
        $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
    }

    /**
     * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
     * @param  string $method The API method to call, e.g. 'lists/list'
     * @param  array  $args   An array of arguments to pass to the method. Will be json-encoded for you.
     * @return array          Associative array of json decoded API response.
     */
    public function call($method, $args=array())
    {
        return $this->makeRequest($method, $args);
    }

    /**
     * Performs the underlying HTTP request. Not very exciting
     * @param  string $method The API method to be called
     * @param  array  $args   Assoc array of parameters to be passed
     * @return array          Assoc array of decoded result
     */
    private function makeRequest($method, $args=array())
    {      
        $args['apikey'] = $this->api_key;

        $url = $this->api_endpoint.'/'.$method.'.json';

        if (function_exists('curl_init') && function_exists('curl_setopt')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');       
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $json_data = json_encode($args);
            $result    = file_get_contents($url, null, stream_context_create(array(
                'http' => array(
                    'protocol_version' => 1.1,
                    'user_agent'       => 'PHP-MCAPI/2.0',
                    'method'           => 'POST',
                    'header'           => "Content-type: application/json\r\n".
                                          "Connection: close\r\n" .
                                          "Content-length: " . strlen($json_data) . "\r\n",
                    'content'          => $json_data,
                ),
            )));
        }

        return $result ? json_decode($result, true) : false;
    }
}


下面是我正在使用的当前MailChimp.php文件:

<?php

// Set to "mailchimp" or "file"
$STORE_MODE = "mailchimp";

// MailChimp API Key findable in your Mailchimp's dashboard
$API_KEY =  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XX0";

// MailChimp List ID  findable in your Mailchimp's dashboard
$LIST_ID =  "XXXXXXXXXX";

require('MailChimp.php');

/* ***************************************************** */
// For the part below, no interventions are required
/* ***************************************************** */

if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["email"])) {

    $email = $_POST["email"];

    header('HTTP/1.1 200 OK');
    header('Status: 200 OK');
    header('Content-type: application/json');

    // Checking if the email writing is good
    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

        // The part for the storage in a .txt
        if ($STORE_MODE == "file") {

            // SUCCESS SENDING
            if(@file_put_contents($STORE_FILE, strtolower($email)."\r\n", FILE_APPEND)) {
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => "FileAccessError"
                    ));
            }

        // The part for the storage in Mailchimp
        } elseif ($STORE_MODE == "mailchimp") {

            $MailChimp = new \Drewm\MailChimp($API_KEY);

            $result = $MailChimp->call('lists/subscribe', array(
                        'id'                => $LIST_ID,
                        'email'             => array('email'=>$email),
                        'double_optin'      => false,
                        'update_existing'   => true,
                        'replace_interests' => false,
                        'send_welcome'      => true,
                    ));     

            // SUCCESS SENDING
            if($result["email"] == $email) {        
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => $result["name"]
                    ));
            }
        // ERROR
        } else {
            echo json_encode(array(
                    "status" => "error",
                ));
        }
    // ERROR DURING THE VALIDATION 
    } else {
        echo json_encode(array(
                "status" => "error",
                "type" => "ValidationError"
            ));
    }
} else {
    header('HTTP/1.1 403 Forbidden');
    header('Status: 403 Forbidden');
}

?>
<?php

namespace Drewm;

class MailChimp
{
    private $api_key;
    private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
    private $verify_ssl   = false;

    /**
     * Create a new instance
     * @param string $api_key Your MailChimp API key
     */
    function __construct($api_key)
    {
        $this->api_key = $api_key;
        list(, $datacentre) = explode('-', $this->api_key);
        $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
    }

    /**
     * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
     * @param  string $method The API method to call, e.g. 'lists/list'
     * @param  array  $args   An array of arguments to pass to the method. Will be json-encoded for you.
     * @return array          Associative array of json decoded API response.
     */
    public function call($method, $args=array())
    {
        return $this->makeRequest($method, $args);
    }

    /**
     * Performs the underlying HTTP request. Not very exciting
     * @param  string $method The API method to be called
     * @param  array  $args   Assoc array of parameters to be passed
     * @return array          Assoc array of decoded result
     */
    private function makeRequest($method, $args=array())
    {      
        $args['apikey'] = $this->api_key;

        $url = $this->api_endpoint.'/'.$method.'.json';

        if (function_exists('curl_init') && function_exists('curl_setopt')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');       
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $json_data = json_encode($args);
            $result    = file_get_contents($url, null, stream_context_create(array(
                'http' => array(
                    'protocol_version' => 1.1,
                    'user_agent'       => 'PHP-MCAPI/2.0',
                    'method'           => 'POST',
                    'header'           => "Content-type: application/json\r\n".
                                          "Connection: close\r\n" .
                                          "Content-length: " . strlen($json_data) . "\r\n",
                    'content'          => $json_data,
                ),
            )));
        }

        return $result ? json_decode($result, true) : false;
    }
}