diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-10-25 18:11:49 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-10-25 18:11:49 -0400 |
commit | a2ebc6c3b2ec3e420a03e36faf00c6e3bf3f25e7 (patch) | |
tree | 55bbe24de52dc14591033cb3f9265acfa74cdbb8 /app/Core/HttpClient.php | |
parent | 6756ef2301a5f624941b947ec9effd34b467de9a (diff) |
Move some classes to namespace Core\Http
Diffstat (limited to 'app/Core/HttpClient.php')
-rw-r--r-- | app/Core/HttpClient.php | 147 |
1 files changed, 0 insertions, 147 deletions
diff --git a/app/Core/HttpClient.php b/app/Core/HttpClient.php deleted file mode 100644 index 7f4ea47a..00000000 --- a/app/Core/HttpClient.php +++ /dev/null @@ -1,147 +0,0 @@ -<?php - -namespace Kanboard\Core; - -/** - * HTTP client - * - * @package core - * @author Frederic Guillot - */ -class HttpClient extends Base -{ - /** - * HTTP connection timeout in seconds - * - * @var integer - */ - const HTTP_TIMEOUT = 5; - - /** - * 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'; - - /** - * Send a GET HTTP request and parse JSON response - * - * @access public - * @param string $url - * @param string[] $headers - * @return array - */ - public function getJson($url, array $headers = array()) - { - $response = $this->doRequest('GET', $url, '', array_merge(array('Accept: application/json'), $headers)); - return json_decode($response, true) ?: array(); - } - - /** - * Send a POST HTTP request encoded in JSON - * - * @access public - * @param string $url - * @param array $data - * @param string[] $headers - * @return string - */ - public function postJson($url, array $data, array $headers = array()) - { - return $this->doRequest( - 'POST', - $url, - json_encode($data), - array_merge(array('Content-type: application/json'), $headers) - ); - } - - /** - * Send a POST HTTP request encoded in www-form-urlencoded - * - * @access public - * @param string $url - * @param array $data - * @param string[] $headers - * @return string - */ - public function postForm($url, array $data, array $headers = array()) - { - return $this->doRequest( - 'POST', - $url, - http_build_query($data), - array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers) - ); - } - - /** - * Make the HTTP request - * - * @access private - * @param string $method - * @param string $url - * @param string $content - * @param string[] $headers - * @return string - */ - private function doRequest($method, $url, $content, array $headers) - { - if (empty($url)) { - return ''; - } - - $default_headers = array( - 'User-Agent: '.self::HTTP_USER_AGENT, - 'Connection: close', - ); - - if (HTTP_PROXY_USERNAME) { - $default_headers[] = 'Proxy-Authorization: Basic '.base64_encode(HTTP_PROXY_USERNAME.':'.HTTP_PROXY_PASSWORD); - } - - $headers = array_merge($default_headers, $headers); - - $context = array( - 'http' => array( - 'method' => $method, - 'protocol_version' => 1.1, - 'timeout' => self::HTTP_TIMEOUT, - 'max_redirects' => self::HTTP_MAX_REDIRECTS, - 'header' => implode("\r\n", $headers), - 'content' => $content - ) - ); - - if (HTTP_PROXY_HOSTNAME) { - $context['http']['proxy'] = 'tcp://'.HTTP_PROXY_HOSTNAME.':'.HTTP_PROXY_PORT; - $context['http']['request_fulluri'] = true; - } - - $stream = @fopen(trim($url), 'r', false, stream_context_create($context)); - $response = ''; - - if (is_resource($stream)) { - $response = stream_get_contents($stream); - } else { - $this->container['logger']->error('HttpClient: request failed'); - } - - if (DEBUG) { - $this->container['logger']->debug('HttpClient: url='.$url); - $this->container['logger']->debug('HttpClient: payload='.$content); - $this->container['logger']->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true)); - $this->container['logger']->debug('HttpClient: response='.$response); - } - - return $response; - } -} |