-
Notifications
You must be signed in to change notification settings - Fork 25
Callbacks
Callbacks (within the context of this library) are functions to be executed upon the completion of a request. There are two kinds of callbacks that can be set, Success Callbacks and Failure Callbacks. In both cases when setting a callback they must fulfill the PHP definition of a callable. It is important to note that the callbacks set on the request object do not persist from one request to the next. Once a request is executed the entire request object will be reset and both the success and failure callbacks will be reset with it.
The PHP definition of a
callablecan be found in this documentation: http://php.net/manual/en/language.types.callable.php
A success callback is a function that will be executed once a response from MailChimp has been evaluated as being a "successful" response. In this context that means that the response contained a http code within the 200 range. When this library receives a response from the MailChimp API it decides weather its a success response or a failure response and returns either a SuccessResponse or FailureResponse object. Both of which are instances of a MailchimpResponse. The callbacks for each are executed in their respective class constructors. Assuming we have a valid instance of the Mailchimp class we can set a success callback like this:
$func = function () {
print " The callback was executed";
};
$mailchimp->request->setSuccessCallback($func);Now when we make a request and the response indicates it was successful the anonymous function $func will be executed.
$mailchimp
->account
->get();
//Outputs: The callback was executedA failure callback is executed when a requests response is interpreted as a failure response. This means that the HTTP code for this response was outside of the 200 range. An example of this might be attempting to retrieve a resource that does not exist (which would typically return a 404). We can set a failure callback like this:
$func = function () {
print " The callback was executed";
};
$mailchimp->request->setFailureCallback($func);Now lets make a request against a resource that does not exist:
$mailchimp
->lists("not list id")
->get();
//Outputs: The callback was executed