-
Notifications
You must be signed in to change notification settings - Fork 24
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
. In the case of a SuccessResponse
or a FailureResponse
, the first and only argument given to your callback will be an instance of the response object itself as shown in the below examples.
The PHP definition of a
callable
can 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 an HTTP code within the 200
range. When this library receives a response from the MailChimp API it decides whether 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 (MailchimpResponse $response) {
print $response->getHttpCode();
};
$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: 200
A failure callback is executed when a 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 (MailchimpResponse $response) {
print $response->getHttpCode();
};
$mailchimp->request->setFailureCallback($func);
Now lets make a request against a resource that does not exist:
$mailchimp
->lists("not list id")
->get();
//Outputs: 404