Batches
Opened this issue · 2 comments
Hello!
I'm not sure if I'm just being an idiot, but I can't get batches to work. Any idea why this code would always return an error 400, 'Schema describes object, array found instead'?
public function update(Request $request) {
$orders = $request->orders;
$list = $request->list;
$operations = [];
foreach($orders as $order) {
$data = [
'email_address' => $order['email'],
'status' => 'Subscribed'
];
$operations[] = [
'method' => 'POST',
'path' => 'lists/' . $list . '/members',
'body' => json_encode($data)
];
}
$response = $this->mc->request('batches', [
$operations
], 'post');
return Response::json([
'message' => $response
], 200);
}
Ok, I've managed to get it to work, but there's still a weird issue. The code I changed was:
$arguments = [
'operations' => $operations
];
$response = $this->mc->request('batches', $arguments, 'POST');
The Mailchimp API accepts that, but no matter what I pass in the operations array, I get this as a response:
_links: [{rel: "parent", href: "https://us4.api.mailchimp.com/3.0/batches", method: "GET",…},…]
0: {rel: "parent", href: "https://us4.api.mailchimp.com/3.0/batches", method: "GET",…}
1: {rel: "self", href: "https://us4.api.mailchimp.com/3.0/batches/9385d86f1c", method: "GET",…}
completed_at: ""
errored_operations: 0
finished_operations: 0
id: "9385d86f1c"
response_body_url: ""
status: "pending"
submitted_at: "2016-02-01T21:56:09+00:00"
total_operations: 0
Hi @mcblum,
Not sure if you still need it, but this is how I got it working, see below. The $batch variable is a Collection holding customer information in this case.
// Build operations
$operations = new Collection();
$batch->each(function(array $customer) use ($operations) {
$operations->push(new \ArrayObject([
'method' => 'PATCH',
'path' => '/lists/' . $this->listId . '/members/' . $customer['mc_id'],
'body' => json_encode([
'merge_fields' => $customer['merge'],
]),
]));
});
// Send operations
$operation = $this->mc->post('/batches', ['operations' => $operations->toArray()]);
// Ask for updates until its completed (every 3 seconds)
while($operation->get('status') !== 'finished') {
print_r($operation);
sleep(3);
$operation = $this->mc->request('/batches/' . $operation->get('id'));
}
What I noticed, is that one the first call, the 'total_operations' tag is always 0. Until the status turns into 'started', it stays 0 in here, but as soon as it started it turned into the number of the operations I'm inserting.