Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di allin81
    Registrato dal
    Mar 2011
    Messaggi
    1,875

    mailchimp: inserire tag automaticamente tramite api

    Buongiorno
    Sto inserendo mailchimp su un sito per poter raccogliere le email di chi vuole registrarsi alla newsletter.

    Riesco a fare tutto senza problemi con un modulo custom utilizzando le api.
    Vorrei solo aggiungere in automatico il tags ma non capisco come posso fare.

    Questo è il codice che ho fino ad ora:
    codice:
    
    $email = $_POST["email"];
    $list_id = 'id della lista dove far finire l'indirizzo email';
    $api_key = 'mia api key';
     
    $data_center = substr($api_key,strpos($api_key,'-')+1);
     
    $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';
     
    $json = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', //pass 'subscribed' or 'pending',
    ]);
     
    try {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        $result = curl_exec($ch);
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
     
        if (200 == $status_code) {
            echo "<div class=\"alert alert-success\" role=\"alert\">Grazie per esserti registrato</div>";
        }
    	else{
            echo "<div class=\"alert alert-danger\" role=\"alert\">email già presente</div>";
        }
    		
    } catch(Exception $e) {
        echo $e->getMessage();
    }
    sto provando a modificare la parte della variabile json cosi' ma non va:
    codice:
    $json = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', 
        'tags'        => 'miotag', 
    ]);
    come potrei fare?
    Grazie

  2. #2
    Prova con:

    codice:
    $json = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', 
        'tags'        => [['status' => 'active', 'name' => 'miotag']], 
    ]);

  3. #3
    Utente di HTML.it L'avatar di allin81
    Registrato dal
    Mar 2011
    Messaggi
    1,875
    Quote Originariamente inviata da filippo.toso Visualizza il messaggio
    Prova con:

    codice:
    $json = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', 
        'tags'        => [['status' => 'active', 'name' => 'miotag']], 
    ]);
    non va, mi da il messaggio di errore (che poi anche io per errore l'ho impostato come "email già presente" ma in realtà è un errore ma non saprei mostrare di che si tratta)

    Ho anche provato a modificare la tua riga da 'active' a 'subscribed' ma niente

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.