summaryrefslogtreecommitdiffstats
path: root/sslLabsApi.php
diff options
context:
space:
mode:
authorbjoernr-de <bjoern.roland@gmail.com>2015-03-25 21:18:12 +0100
committerbjoernr-de <bjoern.roland@gmail.com>2015-03-25 21:18:12 +0100
commit3a77a11a6d2d3f15d4c78d99a5b4acc5e5226c95 (patch)
tree03774d2b903da7f9023c3590b078946035ba9eb0 /sslLabsApi.php
downloadphp-ssllabs-api-3a77a11a6d2d3f15d4c78d99a5b4acc5e5226c95.zip
php-ssllabs-api-3a77a11a6d2d3f15d4c78d99a5b4acc5e5226c95.tar.gz
php-ssllabs-api-3a77a11a6d2d3f15d4c78d99a5b4acc5e5226c95.tar.bz2
Initial commit
Diffstat (limited to 'sslLabsApi.php')
-rw-r--r--sslLabsApi.php174
1 files changed, 174 insertions, 0 deletions
diff --git a/sslLabsApi.php b/sslLabsApi.php
new file mode 100644
index 0000000..f0c5e8d
--- /dev/null
+++ b/sslLabsApi.php
@@ -0,0 +1,174 @@
+<?php
+/**
+ * PHP-SSLLabs-API
+ *
+ * This PHP library provides basic access to the SSL Labs API
+ * and is build upon the official API documentation at
+ * https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
+ *
+ * @author Björn Roland <https://github.com/bjoernr-de>
+ * @license GNU GENERAL PUBLIC LICENSE v3
+ */
+
+class sslLabsApi
+{
+ CONST API_URL = "https://api.ssllabs.com/api/v2";
+
+ /**
+ * sslLabsApi::fetchApiInfo()
+ *
+ * API Call: info
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
+ */
+ public function fetchApiInfo()
+ {
+ return ($this->sendApiRequest('info'));
+ }
+
+ /**
+ * sslLabsApi::fetchHostInformation()
+ *
+ * API Call: analyze
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
+ *
+ * @param string $host Hostname to analyze
+ * @param boolean $publish
+ * @param boolean $startNew
+ * @param boolean $fromCache
+ * @param int $maxAge
+ * @param string $all
+ * @param boolean $ignoreMismatch
+ */
+ public function fetchHostInformation($host, $publish = false, $startNew = false, $fromCache = false, $maxAge = NULL, $all = NULL, $ignoreMismatch = false)
+ {
+ $apiRequest = $this->sendApiRequest
+ (
+ 'analyze',
+ array
+ (
+ 'host' => $host,
+ 'publish' => $publish,
+ 'startNew' => $startNew,
+ 'fromCache' => $fromCache,
+ 'maxAge' => $maxAge,
+ 'all' => $all,
+ 'ignoreMismatch' => $ignoreMismatch
+ )
+ );
+
+ return ($apiRequest);
+ }
+
+ /**
+ * sslLabsApi::fetchHostInformationCached()
+ *
+ * API Call: analyze
+ * Same as fetchHostInformation() but prefer to receive cached information
+ *
+ * @param string $host
+ * @param int $maxAge
+ * @param string $publish
+ * @param string $ignoreMismatch
+ */
+ public function fetchHostInformationCached($host, $maxAge, $publish = false, $ignoreMismatch = false)
+ {
+ return($this->fetchHostInformation($host, $publish, false, true, $maxAge, 'done', $ignoreMismatch));
+ }
+
+ /**
+ * sslLabsApi::fetchEndpointData()
+ *
+ * API Call: getEndpointData
+ * @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
+ *
+ * @param string $host
+ * @param string $s
+ * @param string $fromCache
+ * @return string
+ */
+ public function fetchEndpointData($host, $s, $fromCache = false)
+ {
+ $apiRequest = $this->sendApiRequest
+ (
+ 'getEndpointData',
+ array
+ (
+ 'host' => $host,
+ 's' => $s,
+ 'fromCache' => $fromCache
+ )
+ );
+
+ return ($apiRequest);
+ }
+
+ /**
+ * sslLabsApi::fetchStatusCodes()
+ *
+ * API Call: getStatusCodes
+ */
+ public function fetchStatusCodes()
+ {
+ return ($this->sendApiRequest('getStatusCodes'));
+ }
+
+ /**
+ * sslLabsApi::sendApiRequest()
+ *
+ * Send API request
+ *
+ * @param string $apiCall
+ * @param array $parameters
+ * @return string JSON from API
+ */
+ public function sendApiRequest($apiCall, $parameters = array())
+ {
+ //we also want content from failed api responses
+ $context = stream_context_create
+ (
+ array
+ (
+ 'http' => array
+ (
+ 'ignore_errors' => true
+ )
+ )
+ );
+
+ return (file_get_contents(self::API_URL . '/' . $apiCall . $this->buildGetParameterString($parameters), false, $context));
+ }
+
+ /**
+ * sslLabsApi::buildGetParameterString()
+ *
+ * Helper function to build get parameter string for URL
+ *
+ * @param array $parameters
+ * @return string
+ */
+ 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);
+ }
+} \ No newline at end of file