drewm / mailchimp-api Goto Github PK
View Code? Open in Web Editor NEWSuper-simple, minimum abstraction MailChimp API v3 wrapper, in PHP
License: MIT License
Super-simple, minimum abstraction MailChimp API v3 wrapper, in PHP
License: MIT License
I can't add an user to a list, I don't get any error message.
public function addUserToList ($email, $name, $last_name, $list_id){
$mc = $this->getMailService ();
// List data.
$data = array (
"id" => $list_id,
"email" => array ("email" => $email),
"merge_vars" => array ("FNAME" => $name, "LNAME" => $last_name),
"status" => "subscribed",
"double_optin" => false,
"update_existing" => true,
"replace_interests" => false,
"send_welcome" => false
);
// Insert user into list.
$result = $mc->call ("lists/subscribe", $data);
// Send HTTP response with all data.
var_dump ($result);
}
public function listAction(){
//$this->getCampaigns();
//$this->getList();
$this->addUserToList("[email protected]", "John", "Dole", "29d1ca9e97");
}
I get this response from your api.
array (size=3)
'email' => string '[email protected]' (length=19)
'euid' => string '8757dd3098' (length=10)
'leid' => string '93045953' (length=8)
I check the list using mailchimp panel, and list is empty.
in the background curl is used
require("vendor/autoload.php");
$lukas_api_key = 'c73ec7be5f2edf512565f08d70346742-us12';
$lukas_list_id = '1f246b6cec';
$email = "[email protected]";
$MailChimp = new \Drewm\MailChimp($lukas_api_key);
$isValid = $MailChimp->validateApiKey();
$result = $MailChimp->call('lists/subscribe', array(
'id' => '' . $lukas_list_id . '',
'email' => array('email'=>'[email protected]'),
'merge_vars' => array('FNAME'=>'Somefirst', 'LNAME'=>'Somelast'),
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false,
'send_welcome' => false,
));
print_r($result);
result is null and nothing got added to my list
Hi,
When using the api V3 branch, I had no errors while using GET to retrieve lists.
But when creating a list, the class returns null. Looking into the code shows me that at this line :
$result = curl_exec($ch);
$results contains :
"���<��n� �_�b�%�n蔹�Ե�v�@�I,�@�D�N��urUG��|��ad/h�����ڀ��\������ZK��jC�m�$N('��ٹQ@�Xk��9ffWwk:#$��~��E ���[���+����n�����i�{A��%�6�$��|n1@��#�r:��#W��c�rAO�yx���F�Q������Z���jJ�<�CLO����9Q:݈i�H�k��>g>��N����/P)��('�<����X�H��������z��y�����l���f�"
I don"t know if it is specific to my case or my server, but I prefer to let you know.
I could fix this by adding this line :
curl_setopt($ch, CURLOPT_ENCODING, '');
after :
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
Maybe it will help someone else.
Regards.
After provide API code, how to use your class to get member count of specified list id.
I've confused with call
action & json
Is this a persistent cURL issue with the HTTPS Mailchimp api endpoint? Or is it just me. I changed the MailChimp.php to point to the HTTP endpoint and it worked fine.
Hi,
When there's an error in the API call and you use file_get_content to retrieve data, the 500 http error prevent it to actually retrieve the response explaining the error.
Fix :
add 'ignore_errors' => true to stream context.
Hello, tried the code on localhost and it's working fine. But when deployed to test server, I get error PHP Fatal error: Class 'Drewm\Mailchimp'
What am I doing wrong?
I used composer to install...
and the site is based on wordpress
therefore I call require_once( ABSPATH . "vendor/autoload.php" )
help...
It works perfect for subscribe, but if I try to change the code in:
require_once 'MailChimp.class.php';
$result = $MailChimp->call('lists/unsubscribe', array(
'id' => 'xxx',
'email' => array('email'=>'xxx'),
'delete_member' => false,
'send_goodbye' => false,
'send_notify' => false,
));
print_r($result);
it shows the error: Fatal error: Call to a member function call() on a non-object in /web/htdocs/www.xxx.com on line xx
Am I doing something wrong?
Please help me!
Mailchimp currently recommend the v3 of their API. any plans to make this wrapper or create a new wrapper for the v3?
I've been using this great class for mailchimp api cross talk, but pressure is on to get in support for mergvars that auto create themselves.
On Mailchimp:220
$this->last_request['headers'] = $response['headers']['request_header'];
Hello Drew,
Thank you for this fantastic wrapper! It definitely makes MailChimp API calls a breeze!
I am not sure how happy you are with the repository and whether you are going to make any further changes in the near future, but could you possibly create a tag for the repository so there may be a stable version to refer to in Composer?
dev-master is the only option at the moment.
Thank you in advance!
Best regards,
Edmund
Heya,
Splendid wrapper, thanks alot for putting it out there!
I do have some trouble getting it to send out opt-in, it just adds the email to the list..
$MailChimp = new MailChimp('xxx-xxx');
$result = $MailChimp->post('lists/xxx/members', array(
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array('FNAME'=>$fname, 'LNAME'=>$lname),
'double_optin' => true,
'update_existing' => false,
'replace_interests' => false,
'send_welcome' => true,
));
Does it recognize my IP or something maybe? Any clue?
There is a double slash in the URL the API client calls.
See line 21:
private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0/';
which has an end slash and line 64
$url = $this->api_endpoint.'/'.$method.'.json';
which adds a slash in between, the API seems to work just fine, this just isn't that neat.
Alternatively you can just download the MailChimp.php file and include it manually:
include('./Mailchimp.php');
It should be a capital C in MailChimp
Why is the install process stating to use Composer? Composer installs tonnes of other unnecessary files, and all to avoid this line:
include('MailChimp.php');
You also state use it to install dependencies. What dependencies are there for this file, I cant see any.
It looks to me as if we can just upload the file and use this include line, then we can instantiate a new MailChimp class right away.
Composer is over the top for this - unless your app uses composer, then fine, but you should include "normal" instructions. Many users don't use composer, so you are just overcomplicating a simple task.
My two cents!
Better to let the user decode the result themselves or at least provide a parameter. If you want to use this with an ajax call you'll probable end up having re-encoding it.
I am not even able to get lists
using the example shown on the doc. I wrap the instantiation inside a class.
I followed the sample code but received false when called $MailChimp->get('lists')
I am sure the api key is correct, and currently developing under localhost
Removed abusive tirade
I've set composer to prefer "dist". But I get an error:
Failed to download drewm/mailchimp-api from dist: You must enable the openssl extension to download tiles via http...
I really need the dist version.
When I deploy the codes to server PHP cURL got this error:
SSL certificate: unable to get local issuer certificate
Maybe it's better to add CURLOPT_SSL_VERIFYPEER => 0
?
Hi
I've copied your example code but i'm getting the above error, but the email IS valid. Any ideas what's wrong?
Thanks
Hi - thanks for providing this wrapper. I'm upgrading from the v2.0 wrapper, and having some trouble doing a simple update. get('lists') works, so connectivity is there, but the following code:
$MailChimp = new MailChimp('myapicodehere-us12');
$list_id = '24c23b0042';
$subscriber_hash = $MailChimp->subscriberHash('[email protected]');
$result = $MailChimp->patch("lists/$list_id/members/$subscriber_hash", array(
'merge_fields' => array('FNAME'=>'Firstname', 'LNAME'=>'Lastname')
));
print_r($result);
returns following:
Array ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Resource Not Found [status] => 404 [detail] => The requested resource could not be found. [instance] => )
I've triple-checked the list_id, and am reasonably confident it is correct - both from checking the MailChimp website, and checking the returned ID from get('lists').
I should point out that due to version issues on my production server, I've had to comment out the namespace - but as noted, this did not seem to effect the get('lists') and as this is test code, there should not be any namespace collisions.
Any ideas or pointers on what I'm doing wrong would be greatly appreciated!
i tried to install drewn/mailchimp-api with this strings
{
"require": {
"drewm/mailchimp-api": "*"
}
}
and i get this error; Problem 1
- The requested package drewm/mailchimp-api could not be found in any versio
n, there may be a typo in the package name.
Potential causes:
Read http://getcomposer.org/doc/articles/troubleshooting.md for further common
problems.
Is there a way to add the euid or leid I get an error with every api call ?
thanks
Suddenly I keep receiving the following error. It was working before, but suddenly has stopped, even though I have changed nothing. I wonder if the key is not being being sent across probably. The problem happens on two different sites (on different servers).
detail: "Your request did not include an API key."
instance: ""
status: 401
title: "API Key Missing"
type: "http://kb.mailchimp.com/api/error-docs/401-api-key-missing"
Hi, this is nice simple wrapper, thanks!
I want to proccess errors from Mailchimp API as Exceptions. So I created my MailChimp class:
namespace Codemate;
class MailChimp extends \Drewm\MailChimp
{
public function call($method, $args=array())
{
$response = $this->makeRequest($method, $args);
if (isset($response['status']) && $response['status'] == 'error') {
throw new MailChimpException($response['name'].': '.$response['error']);
}
return $response;
}
}
class MailChimpException extends \Exception {}
And than I can use it in my project like this:
try {
$mc = new \Codemate\MailChimp($this->context->parameters['mailchimp']['apiKey']);
$mc->call('lists/subscribe', array(
'id' => $this->context->parameters['mailchimp']['listId'],
'email' => array('email' => $data['email']),
'update_existing' => true
));
} catch (\Codemate\MailChimpException $e) {
if ($e->getMessage()) {
\Nette\Diagnostics\Debugger::log('mailchimp list subscribe error: '.$e->getMessage(), self::ERROR);
$this->flashMessage($e->getMessage(), 'error');
} else {
throw $e;
}
}
Problem is, that your function makeRequest is private, so I cannot overide it, check $response and throw Exceptions in my MailChimp class.
Can you change this function to protected?
Or if you'r interested in throwing Exceptions instead of only returning $response, I can send pull-request with this improvement.
Thanks for the replay!
Hi,
Just a quick note to help anyone else with this issue.
cURL request fails and just returns false on Windows servers when $verify_ssl = true. Change to false to fix.
Had to add code to output cURL error message to work out what the problem was.
Other than that, working nicely with API V3 - thanks.
Hi!
How to update/insert in bacth process? and merge fields
Thanks
Currently, a failed request returns false without any indication as to what the error is. It would be nice if a curl error was returned in this event:
if (!$result) {
echo 'error: '.curl_error($ch);
}
Can this wrapper send emails at all? It works great for adding and updating members, thanks!
I'd like to test the mailchimp api key, to test if the user enter the right one, for example if my api key var is wrong
i have a php notice :
notice: Undefined offset: 1 in /MailChimp.php on line 31
If someone have an idea, maybe be ping the mailchimp API
Thanks
I can't get any version of this to work. Essentially, I want to list all campaigns in a specific folder. (Doesn't work with single quotes around the 749 either.
$MailChimp->call('campaigns/list', array(
'folder_id' => array('folder_id' => 749)
)
);
$MailChimp->call('campaigns/list', array('folder_id' => 749));
Thanks so much for writing this, it has saved me a lot of time. One quick addition - I was having trouble getting my email addresses to update, so added the following to line 100 below case 'delete':
case 'patch':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
break;
Thanks!
in MailChimp.php line 82 - no case for "put"
switch($http_verb) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
break;
case 'get':
$query = http_build_query($args);
curl_setopt($ch, CURLOPT_URL, $url.'?'.$query);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'patch':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
break;
}
Please add composer support.
I'm using this class and in my office and we have to connect CURL through a proxy, I added a method to set up a proxy. It might be useful for somebody else.
Hello,
In my ML I have two groups Interests-> Cars, Bikes, Boats and Sex-> Male,Female
How do I make a call to subscribe to 1or more interests ?? and only 1 Sex.
Sample:
$result = $MailChimp->post('lists/'.$listID.'/members', array(
'email_address' => $email,
'status' => 'pending',
'merge_fields' => array(
'USERID' => $user_id,
'FNAME'=> $first,
'LNAME'=> $last,
'EMAIL' => $email,
'BDAY' => $bday,
'SEX' => $sex
), // merge_fields array end
'interests' => array( 'Cars' => true, 'Bikes' => true, 'Boats' => false ), // interests is name of group
'sex' => array('male' => true, 'female' => false) // sex is name of group
));
Having a couple of issues.
returns an error for missing second argument. It looks like the call method is expecting a "params" value.
something like print_r( $MailChimp->call( 'lists/list', array( 'nothing' => true ) ) ); of course fixes that issue.
It is not until i add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); into the call method that things seem to work.
It appears that since curl 7.10 this settings is default of True. I'm using curl 7.20.0, thus this explicitly needs to be turned off now. I wonder if prior to 7.10 the default was false.
Hi! I have a need to use a simple php mailchimp wrapper quick for subscribing users to lists and sending an intro campaign. Depending on the data entered.
My server is set at php 5.2.17 and can't change it for the moment.
Can I use this? It's basically the best last stable still used by a lot, before moving to 5.3+ for most servers.
Are there php 5.3 dependencies I can change? Or should I try it as is?
Thanks very much for any information, help, light you can shed on this. Can't make them move quite yet.
Best,
-C
Is there a way to install the MC v3 API through composer ?
Currently I'm only able to install the old one...
thanks !
I try to get your package for api v3 with composer, but It will get on api v2. Can I advice me to get it?
Greetings,
I am test-driving the Mailchimp API v3 using your wrapper. Let me start by thanking you for making it available.
I'm getting an error when attempting to update a merge field using PATCH:
$mc->patch("lists/$mailchimp_list_id/merge-fields/" . $field['merge_id'], $data);
This is the payload (var_dump($json_data);
inserted just before curl_exec()
call in MailChimp.php):
{"options":{"choices":{"0":"Aaa","1":"Bbb","2":"Ccc"}},"apikey":"xxxxxx-us5"}
Mailchimp is returning an error, as follows:
[title] => Invalid Resource
[status] => 400
[detail] => The resource submitted could not be validated. For field-specific details, see the 'errors' array.
[errors] => Array
(
[0] => Array
(
[field] => options.choices
[message] => Schema describes array, object found instead
)
)
I suspect this is caused by use of JSON_FORCE_OBJECT in json_encode()
. Not sure why you are using this option.
As a side note, removing JSON_FORCE_OBJECT yields a different error, but that one is most likely a Mailchimp issue...
[title] => Bad Request
[status] => 400
[detail] => This resource has not yet implemented array assignment for options.choices.
Hi, I've installed this as per the instructions in the readme but the example code given to retrieve all lists yields a blank page - no output at all. I'm running php5.5 on windows - any ideas where to start? Thanks
Is there a way to pass the signup source so that it doesn't appear as "API - Generic". I'm trying to create a segment based on where the user signs up.
Hi. I want to add my subscriber different segment by signup source. I create two segment in Mailchimp. How can I add that subscribe to different segments? I using API v2.
Hi, I'm using a local WAMP server, php 5.3. I enabled curl even though it said not necessary.
I did not use Composer, just include MailChimp.php, and I get this error:
Fatal error: Class 'MailChimp' not found in ...
I do an echo in the file to make sure it's being included and it is. If I comment out the namespace line I don't get the error anymore but nothing happens either. I mean my form contact form gets posted, does other backend stuff like add form details to my db but that's it.
I'm using the add subscriber example.
help!
Hi,
I have a question, how can I pass query parameters to the endpoint? For example as mentioned on this documentation page:
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/abuse-reports/
Kind regards,
Pim
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.