blob: f8d886c25c0b4587a6b6adeac45ae266e96c9937 (
plain)
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
|
<?php
namespace BjoernrDe\SSLLabsApi\Calls;
use BjoernrDe\SSLLabsApi\Exceptions\ApiErrorException;
class GenericCall
{
CONST API_URL = "https://api.ssllabs.com/api/v2";
CONST DEV_API_URL = "https://api.dev.ssllabs.com/api/v2/";
/**
* Api call
* @var string
*/
private $apiCall;
/**
* Additional parameters for Api call
* i.e. hostname in analyze api call
*
* @var array
*/
private $parameters;
/**
* DEV-Mode on|off
* If true ssllabs dev api is used for api calls
*
* @var boolean
*/
private $devMode = false;
/**
* Class constructor
*
* @param string $apiCall
* @param array $parameters
*/
public function __construct($apiCall, $parameters = array())
{
$this->apiCall = $apiCall;
$this->parameters = $parameters;
}
/**
* Get DEV mode
*
* @return boolean
*/
public function getDevMode()
{
return ($this->devMode);
}
/**
* Set DEV mode on or off
*
* @param boolean $devMode
*/
public function setDevMode($devMode)
{
$this->devMode = (boolean) $devMode;
}
/**
* Send request
*
* @throws ApiErrorException
* @return string|boolean JSON Api reponse or false
*/
public function send()
{
if (!empty($this->apiCall))
{
//we also want content from failed api responses
$context = stream_context_create
(
array
(
'http' => array
(
'ignore_errors' => true,
'timeout' => 5
)
)
);
$apiUrl = $this->getDevMode() ? self::DEV_API_URL : self::API_URL;
$apiResponse = @file_get_contents($apiUrl . '/' . $this->apiCall . $this->buildGetParameterString($this->parameters), false, $context);
if ($apiResponse === false)
{
throw new ApiErrorException('No response from API');
}
return ($apiResponse);
}
return (false);
}
/**
* Build GET parameter string for API
* + performs boolean to string (true = 'on' ; false = 'off') operation
*
* @param array $parameters
*/
private function buildGetParameterString($parameters)
{
$string = '';
$counter = 0;
foreach ($parameters as $name => $value)
{
if (!is_string($name) || (!is_string($value) && !is_bool($value) && !is_int($value)))
{
continue;
}
if (is_bool($value))
{
$value = ($value) ? 'on' : 'off';
}
$string .= ($counter == 0) ? '?' : '&';
$string .= urlencode($name) . '=' . urlencode($value);
$counter++;
}
return ($string);
}
}
|