Skip to content

Commit 6a992c4

Browse files
committed
Improve error handling (especially for cURL / HTTP status code errors), add shebang line, check if cURL PHP extension is installed
1 parent df25755 commit 6a992c4

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

functions.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,70 @@
11
<?php
22

3-
require_once 'config.php';
3+
//Try to load required config.php, if it fails, output error, as user probably has not followed "Getting started" guide.
4+
if (!include_once('config.php')) {
5+
outputStderr("Could not open config.php. Please follow the getting started guide and provide a valid config.php file. Exiting.");
6+
die();
7+
}
48

5-
//Declare possbile options
9+
//Declare possible options
610
$quiet = false;
711

812
//Check passed options
913
if(isset($argv)){
10-
foreach ($argv as $option) {
11-
if ($option === "--quiet") {
12-
$quiet = true;
13-
}
14-
}
14+
foreach ($argv as $option) {
15+
if ($option === "--quiet") {
16+
$quiet = true;
17+
}
18+
}
1519
}
1620

1721
const SUCCESS = 'success';
1822

23+
24+
//Checks if curl PHP extension is installed
25+
function _is_curl_installed() {
26+
if (in_array ('curl', get_loaded_extensions())) {
27+
return true;
28+
}
29+
else {
30+
return false;
31+
}
32+
}
33+
1934
// Sends $request to netcup Domain API and returns the result
2035
function sendRequest($request)
2136
{
2237
$ch = curl_init(APIURL);
2338
$curlOptions = array(
2439
CURLOPT_POST => 1,
40+
CURLOPT_TIMEOUT => 30,
2541
CURLOPT_RETURNTRANSFER => 1,
42+
CURLOPT_FAILONERROR => 1,
2643
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
2744
CURLOPT_POSTFIELDS => $request,
2845
);
2946
curl_setopt_array($ch, $curlOptions);
3047

3148
$result = curl_exec($ch);
49+
50+
if (curl_errno($ch)) {
51+
$curl_errno = curl_errno($ch);
52+
$curl_error_msg = curl_error($ch);
53+
}
3254
curl_close($ch);
3355

56+
// Some error handling
57+
if (isset($curl_error_msg)) {
58+
outputStderr("cURL Error: ($curl_errno) $curl_error_msg - Exiting.");
59+
die();
60+
}
61+
62+
if (empty($result)) {
63+
outputStderr("Did not receive a valid response from netcup API (the response was empty). However, I also did not get a curl error or HTTP status code indicating an error. Unknown error. Exiting.");
64+
die();
65+
}
66+
67+
// If everything seems to be ok, proceed...
3468
$result = json_decode($result, true);
3569

3670
return $result;

update.php

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23

34
//Load necessary functions
@@ -8,6 +9,11 @@
89
outputStdout("This script is not affiliated with netcup.");
910
outputStdout("=============================================\n");
1011

12+
if (! _is_curl_installed()) {
13+
outputStderr("cURL PHP extension is not installed. Please install the cURL PHP extension, otherwise the script will not work. Exiting.");
14+
die();
15+
}
16+
1117
outputStdout(sprintf("Updating DNS records for host %s on domain %s\n", HOST, DOMAIN));
1218

1319
// Login

0 commit comments

Comments
 (0)