Passed
Push — master ( e2eed7...6f6e4e )
by Bob
03:02
created

eveApi.php ➔ makeApiRequest()   B

Complexity

Conditions 2
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
cc 2
eloc 16
c 6
b 3
f 0
nc 6
nop 1
dl 0
loc 25
rs 8.8571
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use Monolog\Logger;
27
28
/**
29
 * @param $url
30
 * @return SimpleXMLElement|null
31
 */
32
function makeApiRequest($url)
33
{
34
    $logger = new Logger('eveApi');
35
    $logger->pushHandler(new StreamHandler(__DIR__ . '/log/libraryError.log', Logger::DEBUG));
36
    try {
37
        // Initialize a new request for this URL
38
        $ch = curl_init($url);
39
        // Set the options for this request
40
        curl_setopt_array($ch, array(
41
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
42
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
43
            CURLOPT_SSL_VERIFYPEER => false, // Do not verify the SSL certificate
44
            CURLOPT_TIMEOUT => 15,
45
        ));
46
        // Fetch the data from the URL
47
        $data = curl_exec($ch);
48
        // Close the connection
49
        curl_close($ch);
50
        // Return a new SimpleXMLElement based upon the received data
51
        return new SimpleXMLElement($data);
52
    } catch (Exception $e) {
53
        $logger->error("EVE API Error: " . $e->getMessage());
54
        return null;
55
    }
56
}
57
58
/**
59
 * @return mixed|null
60
 */
61
62
function serverStatus()
63
{
64
    $logger = new Logger('eveApi');
65
    $logger->pushHandler(new StreamHandler(__DIR__ . '/log/libraryError.log', Logger::DEBUG));
66
    try {
67
        // Initialize a new request for this URL
68
        $ch = curl_init("https://api.eveonline.com/server/ServerStatus.xml.aspx");
69
        // Set the options for this request
70
        curl_setopt_array($ch, array(
71
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
72
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
73
            CURLOPT_TIMEOUT => 8,
74
            CURLOPT_SSL_VERIFYPEER => false, // Do not verify the SSL certificate
75
        ));
76
        // Fetch the data from the URL
77
        $data = curl_exec($ch);
78
        // Close the connection
79
        curl_close($ch);
80
81
        $true = "true";
82
        //If server is down return false
83
        if ($data->serverOpen != "True") {
84
            return FALSE;
85
        }
86
        //If server is up return true
87
        return $true;
88
    } catch (Exception $e) {
89
        $logger->error("EVE API Error: " . $e->getMessage());
90
        return null;
91
    }
92
}