From 8dee4334c364508be8b8b32ad6afcce2ec69a7aa Mon Sep 17 00:00:00 2001 From: Florian Boulestreau Date: Mon, 16 Apr 2018 14:26:33 +0200 Subject: [PATCH 1/3] Manage batch errors --- src/Batch.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/MailChimp.php | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Batch.php b/src/Batch.php index b8a5c87..18f9af3 100644 --- a/src/Batch.php +++ b/src/Batch.php @@ -167,4 +167,53 @@ private function queueOperation($http_verb, $id, $method, $args = null) $this->operations[] = $operation; } + + /** + * Get batch errors + * + * @return bool|mixed|null + * @throws \Exception + */ + public function get_errors() + { + if (!extension_loaded('zip')) { + throw new \Exception('get_errors() method need php-zip extension to be installed'); + } + + $status = $this->check_status(); + + if (!isset($status['response_body_url'])) { + return null; + } + + $url = $status['response_body_url']; + $pathBase = '/tmp/archive_'.sha1(time()); + $pathArchive = $pathBase.'.tar.gz'; + $pathZip = $pathBase.'.zip'; + + file_put_contents( $pathArchive, file_get_contents($url) ); + + $p = new \PharData($pathArchive, \RecursiveDirectoryIterator::SKIP_DOTS); + $p->convertToData(\Phar::ZIP); + + $zip = new \ZipArchive; + $res = $zip->open($pathZip); + if ($res === TRUE) { + $zip->extractTo($pathBase); + $zip->close(); + } + + $dir = scandir($pathBase); + $filename = $dir[2]; + + $data = file_get_contents($pathBase.'/'.$filename); + + $response = json_decode($data, true); + + foreach ($response as $i => $r) { + $response[$i]['response'] = json_decode($r['response'], true); + } + + return $response; + } } diff --git a/src/MailChimp.php b/src/MailChimp.php index 87102de..d57551d 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -46,7 +46,7 @@ public function __construct($api_key, $api_endpoint = null) if ($api_endpoint === null) { if (strpos($this->api_key, '-') === false) { - throw new \Exception("Invalid MailChimp API key supplied."); + throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied."); } list(, $data_center) = explode('-', $this->api_key); $this->api_endpoint = str_replace('', $data_center, $this->api_endpoint); From eb6bfebb3037d4f97cff0953dcdcc00de259d919 Mon Sep 17 00:00:00 2001 From: slashome Date: Mon, 16 Apr 2018 14:49:58 +0200 Subject: [PATCH 2/3] Add batch error management --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ee57cc8..7199461 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,13 @@ $result = $Batch->execute(); The result includes a batch ID. At a later point, you can check the status of your batch: +```php +$MailChimp->new_batch($batch_id); +$result = $Batch->get_errors(); +``` + +You can also get the errors from your batch: + ```php $MailChimp->new_batch($batch_id); $result = $Batch->check_status(); From 09a868e53be0a816e679eb993c3394cb3af44fa1 Mon Sep 17 00:00:00 2001 From: slashome Date: Mon, 16 Apr 2018 14:56:39 +0200 Subject: [PATCH 3/3] Reorder readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7199461..e17cb25 100644 --- a/README.md +++ b/README.md @@ -153,14 +153,14 @@ The result includes a batch ID. At a later point, you can check the status of yo ```php $MailChimp->new_batch($batch_id); -$result = $Batch->get_errors(); +$result = $Batch->check_status(); ``` You can also get the errors from your batch: ```php $MailChimp->new_batch($batch_id); -$result = $Batch->check_status(); +$result = $Batch->get_errors(); ``` When your batch is finished, you can download the results from the URL given in the response. In the JSON, the result of each operation will be keyed by the ID you used as the first argument for the request.