{"id":426,"date":"2020-08-12T18:29:35","date_gmt":"2020-08-12T18:29:35","guid":{"rendered":"https:\/\/avantutor.com\/blog\/?p=426"},"modified":"2020-08-12T18:29:43","modified_gmt":"2020-08-12T18:29:43","slug":"subscribe-member-using-mailchimp-api-and-php","status":"publish","type":"post","link":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/","title":{"rendered":"Subscribe Member Using MailChimp API and PHP"},"content":{"rendered":"\n<p>Here is how you can quickly add a member to your Mailchimp mailing list through PHP and using the Mailchimp API. <\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Step 1<\/strong> &#8211; GET API Helper File<\/p>\n\n\n\n<p>Go to <a href=\"https:\/\/github.com\/drewm\/mailchimp-api\">https:\/\/github.com\/drewm\/mailchimp-api<\/a> <\/p>\n\n\n\n<p>Grab the MailChimp.php inside the src folder<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"476\" src=\"https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-1024x476.png\" alt=\"Go to src folder and download MailChimp.php\n\" class=\"wp-image-427\" srcset=\"https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-1024x476.png 1024w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-300x140.png 300w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-768x357.png 768w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-1536x715.png 1536w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-2048x953.png 2048w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-945x440.png 945w, https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-600x279.png 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Place it in the same directory where you run your PHP code that handles your form submissions (if you have such an action).<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>Step 2<\/strong> &#8211; Hide Your MailChimp API Key + List ID<\/p>\n\n\n\n<p>create a keys.php file somewhere outside of the public folder. In my case I had my website in \/home\/freeirvl\/public_html so I placed the keys.php in \/home\/freeirvl <\/p>\n\n\n\n<p>Your keys.php should look something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$mailchimp_api_key = 'YOUR KEY HERE';\n$mailchimp_list_id = 'YOUR LIST ID HERE';\n?><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>Step <\/strong>3 &#8211; The Actual PHP Code!<\/p>\n\n\n\n<p>In whatever file you are trying to add a member to your MailChimp mailing list do the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Load credentials file\r\n  require_once '\/home\/freeirvl\/keys.php';<\/code><\/pre>\n\n\n\n<p>Load the MailChimp helper\/wrapper file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ************************************ LOAD MAILCHIMP LIBRARY************************************\r\n  include(dirname(__FILE__) . '\/MailChimp.php');\r\n  use \\DrewM\\MailChimp\\MailChimp;\r\n  \/\/ ************************************ END LOAD MAILCHIMP ************************************<\/code><\/pre>\n\n\n\n<p>The function that will actually try add a member using the helper\/wrapper file. Feel free to change the argument list. In the following example I decided to take in an email address ($email), first name ($first_name) and last name ($last_name). You might want to leave out first name and last name. <\/p>\n\n\n\n<p>Also don&#8217;t forget to actually <strong>CALL<\/strong> on this function somewhere in your code and provide it with the correct values! <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ************************************ MAILCHIMP ************************************\r\n  function subscribe_add_tags($email, $first_name, $last_name)  {\r\n    \/\/ *************** LOADING API KEY AND LIST ID\r\n    global  $mailchimp_api_key, $mailchimp_list_id;\r\n\r\n    $MailChimp = new MailChimp($mailchimp_api_key);\r\n    $list_id = $mailchimp_list_id;  \/\/  &lt;--- PLAYGROUND LIST_ID\r\n    $hashed= md5(strtolower($email));\r\n    \/\/ *************** CHANGE TAG NAME HERE\r\n    $payload = '{ \"tags\": &#91; { \"name\": \"Bootcamp\", \"status\": \"active\" }  ] }';\r\n\r\n    $payload_json = json_decode($payload, true);\r\n    $result_subscribe = $MailChimp->post(\"lists\/$list_id\/members\", &#91;\r\n\t\t\t\t'email_address' => $email,\r\n\t\t\t\t'status'        => 'subscribed',\r\n        'merge_fields'  => array(\r\n          'FNAME' => $first_name,\r\n          'LNAME' => $last_name,\r\n        )\r\n\t\t\t]);\r\n    $result_tag = $MailChimp->post(\"lists\/$list_id\/members\/$hashed\/tags\", $payload_json);\r\n\r\n\r\n\r\n    \/\/   this error success produces NOTHING in the Response from MailChimp\r\n    \/\/ because it doesn't seem to send anything, but if not error then success I guess\r\n    \/\/ so you can still capture its status\r\n    if ($MailChimp->success()) {\r\n        print_r($result_subscribe);\r\n        print_r($result_tag);\r\n    } else {\r\n        echo 'Error Subscribing and or Tagging Mailchimp&lt;br>';\r\n        print_r($result_subscribe);\r\n        print_r($result_tag);\r\n        echo $MailChimp->getLastError();\r\n    }\r\n  }  \/\/ end function\r\n\/\/ ************************************ END MAILCHIMP ************************************\r\n<\/code><\/pre>\n\n\n\n<p>That is it. <\/p>\n\n\n\n<p>Notes: I am <strong>adding a tag<\/strong> to this member! Also, google how to obtain the MailChimp mailing list id if you are confused about what that is exactly. I have also created a video version of this tutorial. <\/p>\n\n\n\n<p>Let me know your thoughts or if I forgot to mention anything. <\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Using PHP and Mailchimp API to Subscribe and Tag Member\" width=\"676\" height=\"507\" src=\"https:\/\/www.youtube.com\/embed\/hhjUeoMTbJY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><figcaption>Subscribe  and Tag Member Using MailChimp API and PHP<\/figcaption><\/figure>\n\n\n\n<p>For tutoring feel free to visit my tutoring site <a href=\"https:\/\/avantutor.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/avantutor.com<\/a> or post your questions on <a href=\"https:\/\/askavan.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/askavan.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how you can quickly add a member to your Mailchimp mailing list through PHP and using the Mailchimp API. Step 1 &#8211; GET API Helper File Go to https:\/\/github.com\/drewm\/mailchimp-api Grab the MailChimp.php inside the src folder Place it&#8230; <a class=\"more-link\" href=\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[8,39],"tags":[62,47],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding<\/title>\n<meta name=\"description\" content=\"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding\" \/>\n<meta property=\"og:description\" content=\"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\" \/>\n<meta property=\"og:site_name\" content=\"AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-12T18:29:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-12T18:29:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-1024x476.png\" \/>\n<meta name=\"author\" content=\"avansardar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"avansardar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\",\"url\":\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\",\"name\":\"Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding\",\"isPartOf\":{\"@id\":\"https:\/\/avantutor.com\/blog\/#website\"},\"datePublished\":\"2020-08-12T18:29:35+00:00\",\"dateModified\":\"2020-08-12T18:29:43+00:00\",\"author\":{\"@id\":\"https:\/\/avantutor.com\/blog\/#\/schema\/person\/3a1820bcdd71870ace675436f371be9e\"},\"description\":\"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.\",\"breadcrumb\":{\"@id\":\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/avantutor.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Subscribe Member Using MailChimp API and PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/avantutor.com\/blog\/#website\",\"url\":\"https:\/\/avantutor.com\/blog\/\",\"name\":\"AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding\",\"description\":\"Looking for expert advice on how to improve your coding skills? The AvanTutor blog provides a wealth of resources and insights to help you become a better programmer. Our experienced tutors share tips and tricks for mastering popular programming languages such as Java, and JavaScript, as well as insights into the latest trends in software development. With regular updates and engaging content, the AvanTutor blog is your go-to resource for all things coding.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/avantutor.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/avantutor.com\/blog\/#\/schema\/person\/3a1820bcdd71870ace675436f371be9e\",\"name\":\"avansardar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/avantutor.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/81392f2a2c93b7b1c7479ed6b4115f02?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/81392f2a2c93b7b1c7479ed6b4115f02?s=96&d=mm&r=g\",\"caption\":\"avansardar\"},\"url\":\"https:\/\/avantutor.com\/blog\/author\/avansardar\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding","description":"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/","og_locale":"en_US","og_type":"article","og_title":"Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding","og_description":"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.","og_url":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/","og_site_name":"AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding","article_published_time":"2020-08-12T18:29:35+00:00","article_modified_time":"2020-08-12T18:29:43+00:00","og_image":[{"url":"https:\/\/avantutor.com\/blog\/wp-content\/uploads\/2020\/08\/MailChimp-PHP-File-1024x476.png"}],"author":"avansardar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"avansardar","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/","url":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/","name":"Subscribe Member Using MailChimp API and PHP - AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding","isPartOf":{"@id":"https:\/\/avantutor.com\/blog\/#website"},"datePublished":"2020-08-12T18:29:35+00:00","dateModified":"2020-08-12T18:29:43+00:00","author":{"@id":"https:\/\/avantutor.com\/blog\/#\/schema\/person\/3a1820bcdd71870ace675436f371be9e"},"description":"Find out how to quickly add and tag a member to your MailChimp mailing list using the MailChimp API and PHP.","breadcrumb":{"@id":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/avantutor.com\/blog\/subscribe-member-using-mailchimp-api-and-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/avantutor.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Subscribe Member Using MailChimp API and PHP"}]},{"@type":"WebSite","@id":"https:\/\/avantutor.com\/blog\/#website","url":"https:\/\/avantutor.com\/blog\/","name":"AvanTutor Blog - Tips, Tricks, and Resources for Mastering Coding","description":"Looking for expert advice on how to improve your coding skills? The AvanTutor blog provides a wealth of resources and insights to help you become a better programmer. Our experienced tutors share tips and tricks for mastering popular programming languages such as Java, and JavaScript, as well as insights into the latest trends in software development. With regular updates and engaging content, the AvanTutor blog is your go-to resource for all things coding.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/avantutor.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/avantutor.com\/blog\/#\/schema\/person\/3a1820bcdd71870ace675436f371be9e","name":"avansardar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/avantutor.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/81392f2a2c93b7b1c7479ed6b4115f02?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/81392f2a2c93b7b1c7479ed6b4115f02?s=96&d=mm&r=g","caption":"avansardar"},"url":"https:\/\/avantutor.com\/blog\/author\/avansardar\/"}]}},"_links":{"self":[{"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/posts\/426"}],"collection":[{"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":3,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/posts\/426\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avantutor.com\/blog\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}