-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-update.php
158 lines (132 loc) · 4.68 KB
/
contact-update.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
// Name: contact-update.php
// Purpose: This PHP script utilises the SiteHost API to update the contacts for Domains in bulk.
// It is an example script that a customer can customise to their specific needs.
// Author: SiteHost
// API Key
// Can be sourced from the SiteHost Control Panel
//
// Instructions for generating a SiteHost API Key
// https://kb.sitehost.nz/developers/api#creating-an-api-key
//
$API_KEY = "";
// Set the current API Version
$API_VERSION = "1.3";
// Base URL
$BASE_URL = "https://api.sitehost.nz/{$API_VERSION}";
// Visible in the SiteHost Control Panel next to Client name with a # prefix.
$client_id = "12345";
// Contact IDs
$contact_ids = array(
"billing" => "1235",
"technical" => "1235",
"admin" => "12345",
"registrant" => "12345"
);
// Querries the SiteHost API and returns a the JSON dictionary.
function getAPI_JSON($uri) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
curl_close($ch);
return ['error' => "cURL error: $error"];
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status < 200 || $status >= 300) {
return ['error' => "HTTP error: $status"];
}
return json_decode($result, true) ?: ['error' => 'JSON decode error'];
}
// Posts to the SiteHost API and returns a the JSON dictionary.
function postAPI_JSON($uri, $body) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
curl_close($ch);
return ['error' => "cURL error: $error"];
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status < 200 || $status >= 300) {
return ['error' => "HTTP error: $status"];
}
return json_decode($result, true) ?: ['error' => 'JSON decode error'];
}
// Simple api.sitehost.nz request generator for list_accounts and list_domains
function genURI($request, $base_url, $api_key, $client_id, $contact_id='') {
if($request == 'list_domains') {
return("{$base_url}/srs/{$request}.json?apikey={$api_key}&client_id={$client_id}&filters[state]=Active");
}
elseif($request == 'get_contact') {
return("{$base_url}/srs/{$request}.json?apikey={$api_key}&client_id={$client_id}&contact_id={$contact_id}");
}
elseif($request == 'update_domain_contacts') {
return("{$base_url}/srs/{$request}.json");
}
else {
return("{$base_url}/srs/{$request}.json?apikey={$api_key}&client_id={$client_id}");
}
}
echo "Please check over the contacts below before proceeding." . PHP_EOL;
foreach ($contact_ids as $key => $contact_id) {
$contact = getAPI_JSON(genURI('get_contact', $BASE_URL, $API_KEY, $client_id, $contact_id));
echo $key . PHP_EOL;
if (in_array('return', $contact)) {
print_r($contact['return']) . PHP_EOL;
} else {
echo "Error: " . $contact['error'] . PHP_EOL;
exit;
}
}
$domains = getAPI_JSON(genURI('list_domains', $BASE_URL, $API_KEY, $client_id));
echo "Please check over the domains below before proceeding." . PHP_EOL;
if (in_array('return', $domains)) {
foreach ($domains['return']['data'] as $dm_resp) {
echo $dm_resp['domain'] . PHP_EOL;
}
} else {
echo "Error: " . $domains['error'] . PHP_EOL;
exit;
}
echo "Please confirm that you want to proceed: [y/n]";
$input = trim(fgets(STDIN));
if ($input == 'y') {
echo "Proceeding..." . PHP_EOL;
} else {
echo "Exiting..." . PHP_EOL;
exit;
}
// Iterate through every domain in the account
if(in_array('status', $domains)) {
foreach ($domains['return']['data'] as $dm_resp) {
$body = array(
'apikey' => $API_KEY,
'client_id' => $client_id,
'domain' => $dm_resp['domain'],
'registrant_contact_id' => $contact_ids['registrant'],
'admin_contact_id' => $contact_ids['admin'],
'technical_contact_id' => $contact_ids['technical'],
'billing_contact_id' => $contact_ids['billing'],
);
$update = postAPI_JSON(genURI('update_domain_contacts', $BASE_URL, $API_KEY, $client_id), $body);
if (in_array('return', $update)) {
echo "Update contacts for " . $dm_resp['domain'] . ": " . $update['msg'] . PHP_EOL;
} else {
echo "Error: " . $update['error'] . PHP_EOL;
}
// Sleep to avoid rate-limiting
usleep(100000);
}
} else {
var_dump($domains);
}
?>