Ciao a tutti, premettendo che sono un neofita non riesco a capire come mai se ho collegato - inserendo l'apposita API attivata ed il codice della lista contatti - i dati di Mailchimp al mio modulo di iscrizione alla newsletter non succede nulla, per la precisione premo il mio pulsante "vai" ma non succede nulla. Se vado a controllare nella lista contatti in effetti non è stato aggiunto nessun indirizzo eMail.
Riporto il codice del file "newsletter-subscribe.php" a cui è collegato il pulsante:
Codice PHP:
<?php/*Name: Newsletter SubscribeWritten by: Okler Themes - (http://www.okler.net)Version: 3.7.0*/
require_once('mailchimp/mailchimp.php');
// Step 1 - Set the apiKey - How get your Mailchimp API KEY - http://kb.mailchimp.com/article/where-can-i-find-my-api-key$apiKey = '2d3001740c6f6ce1159ae7fc01d45cff-us11'
// Step 2 - Set the listId - How to get your Mailchimp LIST ID - http://kb.mailchimp.com/article/how-can-i-find-my-list-id$listId = '14480b9d95'
// Step 3 (Optional) - Vars - More Information - http://kb.mailchimp.com/merge-tags/using/getting-started-with-merge-tags$mergeVars = array('FNAME'=>'');
$MailChimp = new \Drewm\MailChimp($apiKey);
$result = $MailChimp->call('lists/subscribe', array( 'id' => $listId, 'email' => array('email'=>$_POST['email']), 'merge_vars' => $mergeVars, 'double_optin' => false, 'update_existing' => true, 'replace_interests' => false, 'send_welcome' => false, ));
if (in_array('error', $result)) { $arrResult = array ('response'=>'error','message'=>$result['error']);} else { $arrResult = array ('response'=>'success');}
echo json_encode($arrResult);
Questo invece è il file "mailchimp.php":
Codice PHP:
<?php
namespace Drewm;
/**
* Super-simple, minimum abstraction MailChimp API v2 wrapper
*
* Uses curl if available, falls back to file_get_contents and HTTP stream.
* This probably has more comments than code.
*
* Contributors:
* Michael Minor <me@pixelbacon.com>
* Lorna Jane Mitchell, github.com/lornajane
*
* @author Drew McLellan <drew.mclellan@gmail.com>
* @version 1.1.1
*/
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 = 2d3001740c6f6ce1159ae7fc01d45cff-us11;
list(, http://us2.api.mailchimp.com/1.3/?method=listSubscribe) = 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(), $timeout = 10)
{
return $this->makeRequest($method, $args, $timeout);
}
/**
* 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(), $timeout = 10)
{
$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, $timeout);
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;
}
}
E questo è il codice del mio modulo di iscrizione presente nel mio sito:
codice:
</div> <div class="col-md-3">
<div class="newsletter">
<h4>Newsletter</h4>
<p>Iscriviti alla nostra di newsletter per essere informato sulle nuove promozioni:</p>
<div class="alert alert-success hidden" id="newsletterSuccess">
<strong>Ottimo!</strong> Sei stato inserito nella nostra Mailing List.
</div>
<div class="alert alert-danger hidden" id="newsletterError"></div>
<form id="newsletterForm" action="php/newsletter-subscribe.php" method="POST">
<div class="input-group">
<input class="form-control" placeholder="Indirizzo eMail" name="newsletterEmail" id="newsletterEmail" type="text">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Vai</button>
</span>
</div>
</form>
</div>
</div>