Advertisement
riccardomel

Mailchimp Engine API v3 PHP

Apr 24th, 2017
1,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2. //Developed by Riccardo Mel
  3. include ("mailchimp-config.php");
  4.  
  5. //Vars - Una volta settato consiglio di cambiare in POST
  6. $action = $_GET["action"];
  7. $email = $_GET["email"];
  8. $fname = $_GET["fname"];
  9. $debug = isset($_GET["debug"])?$_GET["debug"]:0;
  10. $listid = $_GET["listid"];
  11. $auth = base64_encode( 'user:'.$apiKey );
  12.  
  13. if ($debug) {
  14.     echo "Debug On <br><br>";
  15.   echo $action;
  16.   echo "<br><br>";
  17.   echo $email;
  18.   echo "<br><br>";
  19.   echo $fname;
  20.   echo "<br><br>";
  21.   echo $listid;
  22.   echo "<br><br>";
  23. }
  24. if (!isset($email) && $action == "subscribe") {
  25.     echo "Nessuna email specificata con questo metodo <br><br>";
  26.   return;
  27. }
  28.  
  29. //CURL
  30. $ch = curl_init();
  31. curl_setopt($ch, CURLOPT_URL, $server."/lists/".$listid."/members");
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',    'Authorization: Basic '.$auth));
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  35.  
  36. switch($action) {
  37.     case "subscribe":
  38.  
  39.       $data = array(
  40.             'apikey'        => $apiKey,
  41.             'email_address' => $email,
  42.             'status'        => 'subscribed',
  43.             'merge_fields'  => array(
  44.                 'FNAME' => $fname
  45.                 )
  46.             );
  47.       $json_data = json_encode($data);
  48.  
  49.       //NECESSARIO SOLO PER Aggiunta =  POST + PARAMETRI
  50.       curl_setopt($ch, CURLOPT_POST, true);
  51.       curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  52.       //NECESSARIO SOLO PER Aggiunta =  POST + PARAMETRI
  53.  
  54.       //curl_exec
  55.       $result = curl_exec($ch);
  56.  
  57.       if ($debug) {     echo "Aggiunto: ".$fname." <br><br>"; print_r($result); }
  58.  
  59.  
  60.     break;
  61.     case "lists":
  62.  
  63.       $result = curl_exec($ch);
  64.       print_r($result);
  65.  
  66.       if ($debug) {     echo "Lista utenti mailchimp <br><br>";  }
  67.  
  68.  
  69.     break;
  70.     default:
  71.     echo "Azione non consentita <br><br>";
  72.     break;
  73. }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement