1
+ <?php
2
+
3
+ class SslLabs
4
+ {
5
+ CONST API_URL = "https://api.ssllabs.com/api/v3 " ;
6
+
7
+ private $ returnObjects ;
8
+
9
+ /**
10
+ * Create a new instance.
11
+ *
12
+ * @param bool $returnObjects Return objects instead of strings from api responses.
13
+ */
14
+ public function __construct (bool $ returnObjects = true )
15
+ {
16
+ $ this ->returnObjects = $ returnObjects ;
17
+ }
18
+
19
+ /**
20
+ * Check SSL Labs availability
21
+ *
22
+ * API Call: info
23
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#check-ssl-labs-availability
24
+ *
25
+ * @return string|object
26
+ */
27
+ public function info ()
28
+ {
29
+ return $ this ->sendApiRequest ('info ' );
30
+ }
31
+
32
+ /**
33
+ * Invoke assessment and check progress
34
+ *
35
+ * API Call: analyze
36
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#invoke-assessment-and-check-progress
37
+ *
38
+ * @param string $host Hostname to analyze
39
+ * @param bool $publish Publish results on ssllabs.com
40
+ * @param bool $startNew Force new report
41
+ * @param bool $fromCache Enable cache
42
+ * @param int $maxAge Cached age in hours
43
+ * @param string $all ("on"|"off"|"done")
44
+ * @param bool $ignoreMismatch Proceed even when the hostname and certificate doesn't match
45
+ * @return string|object
46
+ */
47
+ public function analyze (string $ host , bool $ publish = false , bool $ startNew = false , bool $ fromCache = false ,
48
+ int $ maxAge = null , string $ all = null , bool $ ignoreMismatch = false )
49
+ {
50
+ return $ this ->sendApiRequest (
51
+ 'analyze ' ,
52
+ [
53
+ 'host ' => $ host ,
54
+ 'publish ' => $ publish ,
55
+ 'startNew ' => $ startNew ,
56
+ 'fromCache ' => $ fromCache ,
57
+ 'maxAge ' => $ maxAge ,
58
+ 'all ' => $ all ,
59
+ 'ignoreMismatch ' => $ ignoreMismatch ,
60
+ ]
61
+ );
62
+ }
63
+
64
+ /**
65
+ * Same as analyze but prefer to receive cached information
66
+ *
67
+ * API Call: analyze
68
+ *
69
+ * @param string $host
70
+ * @param bool $publish
71
+ * @param int $maxAge
72
+ * @param string $all
73
+ * @param bool $ignoreMismatch
74
+ * @return string|object
75
+ */
76
+ public function analyzeCached (string $ host , bool $ publish = false , int $ maxAge = 24 , string $ all = 'done ' , bool $ ignoreMismatch = false )
77
+ {
78
+ return $ this ->analyze ($ host , $ publish , false , true , $ maxAge , $ all , $ ignoreMismatch );
79
+ }
80
+
81
+ /**
82
+ * Retrieve detailed endpoint information
83
+ *
84
+ * API Call: getEndpointData
85
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#retrieve-detailed-endpoint-information
86
+ *
87
+ * @param string $host
88
+ * @param string $s IP Address
89
+ * @param bool $fromCache Cached results
90
+ * @return string|object
91
+ */
92
+ public function getEndpointData ($ host , $ s , $ fromCache = false )
93
+ {
94
+ $ apiRequest = $ this ->sendApiRequest (
95
+ 'getEndpointData ' ,
96
+ [
97
+ 'host ' => $ host ,
98
+ 's ' => $ s ,
99
+ 'fromCache ' => $ fromCache ,
100
+ ]
101
+ );
102
+
103
+ return $ apiRequest ;
104
+ }
105
+
106
+ /**
107
+ * Retrieve known status codes
108
+ *
109
+ * API Call: getStatusCodes
110
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#retrieve-known-status-codes
111
+ *
112
+ * @return string|object
113
+ */
114
+ public function getStatusCodes ()
115
+ {
116
+ return $ this ->sendApiRequest ('getStatusCodes ' );
117
+ }
118
+
119
+ /**
120
+ * Retrieve root certificates
121
+ *
122
+ * API Call: getRootCertsRaw
123
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#retrieve-root-certificates
124
+ *
125
+ * @return string|object
126
+ */
127
+ public function getRootCertsRaw ()
128
+ {
129
+ return $ this ->sendApiRequest ('getRootCertsRaw ' );
130
+ }
131
+
132
+ /**
133
+ * Send API request
134
+ *
135
+ * @param string $apiCall
136
+ * @param array $parameters
137
+ * @return string|object JSON from API
138
+ */
139
+ public function sendApiRequest (string $ apiCall , array $ parameters = [])
140
+ {
141
+ // We also want content from failed api responses
142
+ $ context = stream_context_create (
143
+ [
144
+ 'http ' => [
145
+ 'ignore_errors ' => true
146
+ ],
147
+ ]
148
+ );
149
+
150
+ $ url = self ::API_URL . '/ ' . $ apiCall . $ this ->buildGetParameterString ($ parameters );
151
+
152
+ $ apiResponse = file_get_contents (
153
+ $ url ,
154
+ false ,
155
+ $ context
156
+ );
157
+
158
+ if ($ this ->returnObjects === true ) {
159
+ return json_decode ($ apiResponse );
160
+ }
161
+
162
+ return $ apiResponse ;
163
+ }
164
+
165
+ /**
166
+ * Set true to return all API responses as JSON object, false returns it as simple JSON strings
167
+ *
168
+ * @param bool $returnObjects
169
+ */
170
+ public function setReturnJsonObjects (bool $ returnObjects )
171
+ {
172
+ $ this ->returnObjects = $ returnObjects ;
173
+ }
174
+
175
+ /**
176
+ * Getter for returnObjects
177
+ *
178
+ * @return bool true returns all API responses as JSON object, false returns it as simple JSON string
179
+ */
180
+ public function getReturnJsonObjects ()
181
+ {
182
+ return $ this ->returnObjects ;
183
+ }
184
+
185
+ /**
186
+ * Helper function to build get parameter string for URL
187
+ *
188
+ * @param array $parameters
189
+ * @return string
190
+ */
191
+ private function buildGetParameterString (array $ parameters )
192
+ {
193
+ $ string = '' ;
194
+
195
+ $ counter = 0 ;
196
+ foreach ($ parameters as $ name => $ value ) {
197
+ if (!is_string ($ name ) || (!is_string ($ value ) && !is_bool ($ value ) && !is_int ($ value ))) {
198
+ continue ;
199
+ }
200
+
201
+ if (is_bool ($ value )) {
202
+ $ value = ($ value ) ? 'on ' : 'off ' ;
203
+ }
204
+
205
+ $ string .= ($ counter == 0 ) ? '? ' : '& ' ;
206
+ $ string .= urlencode ($ name ) . '= ' . urlencode ($ value );
207
+
208
+ $ counter ++;
209
+ }
210
+
211
+ return $ string ;
212
+ }
213
+ }
0 commit comments