|
1 | 1 | <?php
|
2 | 2 |
|
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 | +} |
4 | 8 |
|
5 |
| -//Declare possbile options |
| 9 | +//Declare possible options |
6 | 10 | $quiet = false;
|
7 | 11 |
|
8 | 12 | //Check passed options
|
9 | 13 | 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 | + } |
15 | 19 | }
|
16 | 20 |
|
17 | 21 | const SUCCESS = 'success';
|
18 | 22 |
|
| 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 | + |
19 | 34 | // Sends $request to netcup Domain API and returns the result
|
20 | 35 | function sendRequest($request)
|
21 | 36 | {
|
22 | 37 | $ch = curl_init(APIURL);
|
23 | 38 | $curlOptions = array(
|
24 | 39 | CURLOPT_POST => 1,
|
| 40 | + CURLOPT_TIMEOUT => 30, |
25 | 41 | CURLOPT_RETURNTRANSFER => 1,
|
| 42 | + CURLOPT_FAILONERROR => 1, |
26 | 43 | CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
|
27 | 44 | CURLOPT_POSTFIELDS => $request,
|
28 | 45 | );
|
29 | 46 | curl_setopt_array($ch, $curlOptions);
|
30 | 47 |
|
31 | 48 | $result = curl_exec($ch);
|
| 49 | + |
| 50 | + if (curl_errno($ch)) { |
| 51 | + $curl_errno = curl_errno($ch); |
| 52 | + $curl_error_msg = curl_error($ch); |
| 53 | + } |
32 | 54 | curl_close($ch);
|
33 | 55 |
|
| 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... |
34 | 68 | $result = json_decode($result, true);
|
35 | 69 |
|
36 | 70 | return $result;
|
|
0 commit comments