Skip to content

Commit 9e051da

Browse files
committed
Added composer and upgraded to api v3
* Composer autoloading * SslLabs API v3
1 parent 6817fa2 commit 9e051da

File tree

8 files changed

+336
-223
lines changed

8 files changed

+336
-223
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
/coverage/
3+
4+
composer.lock

CONTRIBUTORS

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Björn Roland <bjoern.roland@gmail.com>
1+
Björn Roland <bjoern.roland@gmail.com>
2+
Tim Helfensdörfer <tim@visualappeal.de>

README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ It's build upon the official API documentation at https://github.com/ssllabs/ssl
55
```PHP
66
<?php
77

8-
require_once 'sslLabsApi.php';
8+
require_once 'vendor/autoload.php';
99

1010
//Return API response as JSON string
11-
$api = new sslLabsApi();
11+
$api = new SslLabs();
1212

1313
//Return API response as JSON object
14-
//$api = new sslLabsApi(true);
15-
16-
//Set content-type header for JSON output
17-
header('Content-Type: application/json');
14+
//$api = new SslLabs(true);
1815

1916
//get API information
2017
var_dump($api->fetchApiInfo());

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "visualappeal/php-ssllabs-api",
3+
"description": "API for ssllabs.com",
4+
"type": "library",
5+
"license": "GPL",
6+
"authors": [
7+
{
8+
"name": "Björn Roland",
9+
"email": "bjoern.roland@gmail.com"
10+
},
11+
{
12+
"name": "Tim Helfensdörfer",
13+
"email": "tim@visualappeal.de"
14+
}
15+
],
16+
"minimum-stability": "stable",
17+
"require": {
18+
"php": ">=7.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"": "src/"
23+
}
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^7.4"
27+
}
28+
}

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
4+
colors="true">
5+
<testsuites>
6+
<testsuite name="SslLab Tests">
7+
<directory>./tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
<filter>
11+
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
</whitelist>
14+
</filter>
15+
<logging>
16+
<log type="coverage-html" target="./coverage" />
17+
</logging>
18+
</phpunit>

src/SslLabs.php

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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

Comments
 (0)