summaryrefslogtreecommitdiff
path: root/app/Core/HttpClient.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Core/HttpClient.php')
-rw-r--r--app/Core/HttpClient.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/app/Core/HttpClient.php b/app/Core/HttpClient.php
new file mode 100644
index 00000000..96860152
--- /dev/null
+++ b/app/Core/HttpClient.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Core;
+
+/**
+ * HTTP client
+ *
+ * @package core
+ * @author Frederic Guillot
+ */
+class HttpClient
+{
+ /**
+ * HTTP connection timeout in seconds
+ *
+ * @var integer
+ */
+ const HTTP_TIMEOUT = 2;
+
+ /**
+ * Number of maximum redirections for the HTTP client
+ *
+ * @var integer
+ */
+ const HTTP_MAX_REDIRECTS = 2;
+
+ /**
+ * HTTP client user agent
+ *
+ * @var string
+ */
+ const HTTP_USER_AGENT = 'Kanboard Webhook';
+
+ /**
+ * Send a POST HTTP request
+ *
+ * @static
+ * @access public
+ * @param string $url
+ * @param array $data
+ * @return string
+ */
+ public static function post($url, array $data)
+ {
+ if (empty($url)) {
+ return '';
+ }
+
+ $headers = array(
+ 'Connection: close',
+ 'User-Agent: '.self::HTTP_USER_AGENT,
+ );
+
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'method' => 'POST',
+ 'protocol_version' => 1.1,
+ 'timeout' => self::HTTP_TIMEOUT,
+ 'max_redirects' => self::HTTP_MAX_REDIRECTS,
+ 'header' => implode("\r\n", $headers),
+ 'content' => json_encode($data)
+ )
+ ));
+
+ return @file_get_contents(trim($url), false, $context);
+ }
+}