-
Notifications
You must be signed in to change notification settings - Fork 24
Post Requests
# Post Requests
When you want to send data to MailChimp, or make a new record of something in MailChimp you will want to send a
POST
request. This can be done through this library by calling the post()
method on any instance of the ApiResounce
class.
The post method takes in a single argument being the JSON
serialize-able parameters you wish to send to MailChimp. Typically this is going to be an array of key-value pairs, but any serialize-able object will work. You can send whatever parameters you would like with a request but it should be noted that some of MailChimps endpoints require specific parameters to be in a request in order for it to be successful. You should always be sure you are send all the required parameters for the resource you are posting.
You can always double check what parameters are required for each resource by viewing the MailChimp API docs: http://developer.mailchimp.com/documentation/mailchimp/reference/overview/
As an example of how to post a resource using this library lets talk through adding a new contact to MailChimp. We can assume we have already correctly instantiated a $mailchimp
instance so our next step is going to be to get the list_id for the list that we want to post to.
You can review how to make
GET
requests here: https://github.com/Jhut89/Mailchimp-API-3.0-PHP/wiki/Get-Requests
We can get the list_id of the list we want by using this libraries get()
method like this:
$lists_response = $mailchimp->lists()->get()
// returns a lists collection
From the above call we can derive which list is the one we want and from there its id. Now that we have the list_id for the list that we want to post to we can start looking at usage of the post()
method. The first thing we should do is to double check what resource we want to post to and if it has any required parameters. We can do this by looking at the docs for the resource that we are wanting to post to. In this case we want to post to the /lists/{list_id}/members
endpoint, documented here: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
We can see from the documentation that the endpoint that we are posting to has two required parameters being email_address
and status
:
Because of this we now know to structure our code like this:
$params = [
"email_address" => "[email protected]",
"status" => "subscribed"
]
$post_response = $mailchimp
->lists($list_id)
->members()
->post($params);
Assuming a successful request we should see a request body returned from MailChimp that looks like this:
{
"id": "852aaa9532cb36adfb5e9fef7a4206a9",
"email_address": "[email protected]",
"unique_email_id": "fab20fa03d",
"email_type": "html",
"status": "subscribed",
"status_if_new": "",
"merge_fields": {
"FNAME": "",
"LNAME": ""
},
"interests": {
"9143cf3bd1": false,
"3a2a927344": false,
"f9c8f5f0ff": false,
"f231b09abc": false,
"bd6e66465f": false
},
"stats": {
"avg_open_rate": 0,
"avg_click_rate": 0
},
"ip_signup": "",
"timestamp_signup": "",
"ip_opt": "198.2.191.34",
"timestamp_opt": "2015-09-16 19:24:29",
"member_rating": 2,
"last_changed": "2015-09-16 19:24:29",
"language": "",
"vip": false,
"email_client": "",
"location": {
"latitude": 0,
"longitude": 0,
"gmtoff": 0,
"dstoff": 0,
"country_code": "",
"timezone": ""
},
"list_id": "57afe96172",
"_links": [...]
}