-
Notifications
You must be signed in to change notification settings - Fork 24
Get Requests
As anywhere else in programming a GET
request is made when requesting information from an external resource and is not traditionally used to alter information or a resource. We make GET
requests using this liberary by executing the get()
method on any instance of an ApiResource
.
Lets assume we have already instantiated our $mailchimp
object using a valid API key, and that we want to get an instance of an e-commerce product associated with our MailChimp account. Lets walk through how we might go about that using this library. First we might need the stores associated with this account as we would need the store_id in order to access the product instance we want:
$response = $mailchimp
->ecommerceStores()
->get();
The MailChimp docs state that the response to a get request against their /ecommerce/stores/
endpoint should return a JSON object like:
{
"stores": [
{
"id": "MC001",
"list_id": "205d96e6b4",
"name": "Freddie's Merchandise",
"domain": "freddiesjokes.com",
"email_address": "[email protected]",
"currency_code": "USD",
"money_format": "",
"primary_locale": "",
"timezone": "Eastern",
"phone": "",
"address": {
"address1": "675 Ponce de Leon Ave NE",
"address2": "Suite 5000",
"city": "Atlanta, GA 30308",
"province": "",
"province_code": "",
"postal_code": "",
"country": "",
"country_code": "",
"longitude": 0,
"latitude": 0
},
"created_at": "2016-02-09T20:26:59+00:00",
"updated_at": "2016-02-09T20:26:59+00:00",
"_links": [...]
}
],
"total_items": 1,
"_links": [...]
}
This means that assuming our request is successful we should be able to get a sercahable/iterable array of stores by calling:
$stores = $response->deserialize()->stores;
From there you are welcome to derive which store is the one you are looking for however you like. Lets assume for the sake of this example that it is the only one in the above response. So now we have access to the store_id
we want through (or however you want to derive it);
$store_id = $stores[0]->id;
Now we can try and get that store products:
$response = $mailchimp
->ecommerceStores($store_id)
->products()
->get();
$products = $response->deserialize()->products;
Something is wrong. You cant find the product you are looking for in the response. You also know you have more that 10
products for this store but for some reason:
print count($products) // outputs 10
This is because the MailChimp API defaults to retrieving only the first 10 instances in any collection. You know you have around 30
products so how do we get the product we are looking for. The MailChimp API docs tell us that we can pass a count
query parameter to request a specific number of products in our response. This library constructs query parameters from an array passed into the get()
method. So we can:
$response = $mailchimp
->ecommerceStores($store_id)
->products()
->get(["count" => 30]);
That will return us all 30 products associated with our store. It should be said that for very large collections its probably best to paginate your requests using a combination of the count
and offset
parameters.
Once we have all of our products we can then derive what the id is for the product we are looking for is and can then retrieve its instance by calling:
$response = $mailchimp
->ecommerceStores($store_id)
->products($product_id)
->get();