MailChimp 3.0 PHP Signup
1.0.0
Mailchimp의 v3.0 API를 사용하는 PHP 메일링 리스트 가입 양식.
Mailchimp v3.0 API를 사용하면 이 양식을 사용하여 사용자가 이메일을 입력하고 '가입'을 클릭하면 추가 창이나 확인 대화 상자가 표시되지 않고 자동으로 이메일 주소가 Mailchimp 목록에 추가됩니다.
HTML/JS 코드:
< script src =" https://code.jquery.com/jquery-1.11.3.min.js " > </ script >
< form id =" signup " action =" index.html " method =" get " >
< input type =" hidden " name =" ajax " value =" true " />
< input type =" email " name =" email " id =" email " placeholder =" Email address " required />
< input type =" submit " id =" SendButton " name =" submit " value =" Sign Up " /> < span id =" message " > </ span >
</ form >
< script >
$ ( document ) . ready ( function ( ) {
$ ( '#signup' ) . submit ( function ( ) {
$ ( "#message" ) . html ( "Adding your email address..." ) ;
$ . ajax ( {
url : '/assets/php/subscribe.php' ,
type : "POST" ,
data : $ ( '#signup' ) . serialize ( ) ,
success : function ( msg )
{
$ ( '#message' ) . html ( "<i class='fa fa-check'></i> Thanks for signing up!" ) ;
} ,
error : function ( msg )
{
$ ( '#message' ) . html ( "<span style='color: red;'><i class='fa fa-times'></i> Error. Please try again later.</span>" ) ;
console . log ( arguments ) ;
}
} ) ;
return false ;
} ) ;
} ) ;
</ script >
관련 PHP 파일은 '/assets/php/'에 있지만 코드는 다음과 같습니다.
<?php
$ data = [
' email ' => $ _POST [ ' email ' ],
' status ' => ' subscribed ' ,
' firstname ' => ' Foo ' ,
' lastname ' => ' Subscriber '
];
syncMailchimp ( $ data );
function syncMailchimp ( $ data ) {
$ apiKey = ' ***YOUR_API_KEY*** ' ;
$ listId = ' ***YOUR_LIST_ID*** ' ;
$ memberId = md5 ( strtolower ( $ data [ ' email ' ]));
$ dataCenter = substr ( $ apiKey , strpos ( $ apiKey , ' - ' )+ 1 );
$ url = ' https:// ' . $ dataCenter . ' .api.mailchimp.com/3.0/lists/ ' . $ listId . ' /members/ ' . $ memberId ;
$ json = json_encode ([
' email_address ' => $ data [ ' email ' ],
' status ' => $ data [ ' status ' ], // "subscribed","unsubscribed","cleaned","pending"
' merge_fields ' => [
' FNAME ' => $ data [ ' firstname ' ],
' LNAME ' => $ data [ ' lastname ' ]
]
]);
$ ch = curl_init ( $ url );
curl_setopt ( $ ch , CURLOPT_USERPWD , ' user: ' . $ apiKey );
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_CUSTOMREQUEST , ' PUT ' );
curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ json );
$ result = curl_exec ( $ ch );
$ httpCode = curl_getinfo ( $ ch , CURLINFO_HTTP_CODE );
curl_close ( $ ch );
return $ httpCode ;
}
?>