Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revision Both sides next revision
daily:mailchimp [2019/03/25 13:19]
admin created
daily:mailchimp [2019/04/10 11:53]
admin
Line 1: Line 1:
-  * Using TAGS+  * Using TAGS and with PHP 
 + 
 +Firt. Function to send the request to mailchimp and get response 
 +<code php> 
 + 
 +function mc_request($api,​ $type, $target, $data = false) { 
 +    $ch = curl_init($api['​url'​] . $target); 
 + 
 +    curl_setopt($ch,​ CURLOPT_HTTPHEADER,​ array 
 +        ( 
 +        '​Content-Type:​ application/​json',​ 
 +        '​Authorization:​ ' . $api['​login'​] . ' ' . $api['​key'​],​ 
 +    )); 
 + 
 +    curl_setopt($ch,​ CURLOPT_CUSTOMREQUEST,​ $type); 
 +    curl_setopt($ch,​ CURLOPT_TIMEOUT,​ 5); 
 +    curl_setopt($ch,​ CURLOPT_RETURNTRANSFER,​ true); 
 +    curl_setopt($ch,​ CURLOPT_USERAGENT,​ '​YOUR-USER-AGENT'​);​ 
 + 
 +    if ($data) 
 +        curl_setopt($ch,​ CURLOPT_POSTFIELDS,​ json_encode($data));​ 
 + 
 +    $response = curl_exec($ch);​ 
 +    curl_close($ch);​ 
 + 
 +    return $response;​ 
 +
 +</​code>​ 
 + 
 + 
 +Then we just generate the values to feed the function: 
 + 
 +<code php> 
 +$tags = array('​values','​to','​tag'​);​ 
 + 
 +   $api = array 
 +        ( 
 +        '​login'​ => '​login',​ 
 +        '​key'​ => 'key from mailchimp',​ 
 +        '​url'​ => '​https://​us20.api.mailchimp.com/​3.0/'​ 
 +    ); 
 + 
 + ​$target = '​lists/​xxxxxxxxxx/​members/';​ //The list we need to work with 
 + 
 +//Values we are going to feed to mailchimp 
 + 
 +   ​$fields = array( 
 +            '​email_address'​ => $email, 
 +            '​status'​ => '​subscribed',​ 
 +            '​merge_fields'​ => array('​FNAME'​ => $name, '​LNAME'​ => $surname),​ 
 +            '​tags'​ =>​$tags);​ 
 +             
 +             
 +            //And send to function 
 +           
 +        echo mc_request($api,​ '​POST',​ $target, $fields); 
 +         
 +</​code>​
  • daily/mailchimp.txt
  • Last modified: 2019/04/10 11:56
  • by admin