forked from mailchimp/mc-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMailchimpResponse.php
69 lines (61 loc) · 1.85 KB
/
getMailchimpResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
if ($argc!=3) {
echo "You must call like:\n\t getMailchimpResponse apikey batchid\n";
exit;
}
$apiKey = $argv[1];
$batchId = $argv[2];
$dc = 'us1';
if (strstr($apiKey, "-")) {
list($key, $dc) = explode("-", $apiKey, 2);
if (!$dc) {
$dc = "us1";
}
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://$dc.api.mailchimp.com/3.0/batches/$batchId",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_USERPWD => "noname:$apiKey",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$jsonResponse = json_decode($response);
if ($jsonResponse->status == 'finished') {
$fileUrl = $jsonResponse->response_body_url;
// check if the file is not expired
parse_str($fileUrl,$fileParams);
try {
$fd = fopen("$batchId.response.tar.gz", 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_FILE, $fd);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
$r = curl_exec($ch);
curl_close($ch);
fclose($fd);
echo "$batchId.response.tar.gz\n";
} catch(Exception $e) {
echo $e->getMessage();
}
}
// else {
// echo "Error: the batch is not finished or have errors\n";
// echo "Error ".$jsonResponse->title." Detail: ".$jsonResponse->detail."\n";
// }
}