我正在为Android应用程序创建一个Web服务
Android应用程序有一个"时事通讯订阅" 3个字段的选项:
- 电子邮件
- 姓
- phone_number
应用程序以json格式将这些数据发送到我的Web服务上的NewsletterControlleur.php
:
{
"Newsletter":{
"user":{
"first_name":"Hugo",
"surname":"Dumoulin",
"phone":"0606060606",
"email":"hugo@gmail.fr"
}
}
}
我现在需要将此人添加到我的Mailchimp订阅者列表中 如何使用我的json数据从控制器执行此操作并且没有任何形式?
我知道我必须将这些数据发布到这样的网址(mailchimp给了我):
HTTP://******.us1.list-manage.com/subscribe/post U = ************和ID = ****** **
有谁知道我怎么能做到这一点?
提前谢谢。
答案 0 :(得分:3)
That list-manage URL is intended to be hit by a user with a browser, and there are several different things that could trip up your process. Specifically, MailChimp can sometimes show a CAPTCHA if it thinks the subscriptions are coming from a bot.
The better way is by using MailChimp's API. You'll need four things:
canEdit: function(inCell, inRowIndex) {
if(inRowIndex === 0) { //Note that you can apply any conditions you want to this
return false;
}
return this._canEdit;
}
and it's at the beginning of the URL when you're logged in to MailChimp. I'll use us7 below, but you should substitute your own.us7
. 1295ff8fdb
, FNAME
, and LNAME
below, but you'll want to substitute your own in order for this to work properly.Once you have those things, you can use any HTTP client (like Cake's) to make the subscription.
v3 of MailChimp's API uses HTTP Basic Authentication. This is a header-based authentication scheme and the vast majority of HTTP libraries support this easily out of the box. If you have to do this by hand, you pass a header named PHONE
and the contents of that header are the word "Basic" followed by a space and then your username and password, separated by a colon, Base64 encoded. For MailChimp, the username is irrelevant and you use your API Key as the password.
Fortunately, Cake's client offers support out of the box. To use it, just pass an Authorization
key into the options parameter of the request. The value should look like this: auth
. (Note, the username is not used by MailChimp, you can pass any string you want.
Using my sample information above, I'd make a array('type' => 'basic', 'username' => 'anything', 'password' => $MAILCHIMP_API_KEY)
call to POST
with a JSON payload that looks like this:
https://us7.api.mailchimp.com/3.0/lists/1295ff8fdb/members/
So, if you're using the HTTP client linked above, the code might look like this:
{
"email_address": "hugo@leggett.fr",
"status": "subscribed",
"merge_fields": {
"FNAME": "Hugo",
"LNAME": "Dumoulin",
"PHONE": "0606060606"
}
}
Now, if the user has already been subscribed, MailChimp will return a 4xx error and this code doesn't account for any other error conditions, so definitely use this as a jumping-off point rather than a complete solution, but it should get you pointed in the right direction.