resq/catalyst/api-bundle/Connector/Client.php

151 lines
4.2 KiB
PHP

<?php
namespace Catalyst\APIBundle\Connector;
use DateTime;
class Client
{
const HEADER_API_KEY = 'X-Cata-API-Key';
const HEADER_SIGNATURE = 'X-Cata-Signature';
const HEADER_DATE = 'X-Cata-Date';
const DATE_FORMAT = 'D, d M Y H:i:s T';
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
protected $protocol;
protected $server;
protected $port;
protected $api_key;
protected $secret_key;
protected $curl;
public function __construct($server, $api_key, $secret_key)
{
$this->protocol = 'https';
$this->port = null;
$this->server = $server;
$this->api_key = $api_key;
$this->secret_key = $secret_key;
$this->curl = curl_init();
}
public function __destruct()
{
curl_close($this->curl);
}
public function setProtocol($protocol)
{
if ($protocol != 'http' && $protocol != 'https')
return $this;
$this->protocol = $protocol;
return $this;
}
protected function getDateString()
{
$date = new DateTime();
return $date->format(self::DATE_FORMAT);
}
public function get($url, $params = [])
{
curl_reset($this->curl);
$date_string = $this->getDateString();
$headers = $this->generateHeaders('GET', $url, $date_string);
// build query string
if (count($params) > 0)
$query_string = '?' . http_build_query($params);
else
$query_string = '';
// build url
if ($this->port == null)
$full_url = $this->protocol . '://' . $this->server . $url . $query_string;
else
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url . $query_string;
error_log($full_url);
// curl
// curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_URL, $full_url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 0);
$res = curl_exec($this->curl);
error_log($res);
}
public function post($url, $params = [])
{
curl_reset($this->curl);
$date_string = $this->getDateString();
$headers = $this->generateHeaders('POST', $url, $date_string);
// build query string
$query_string = http_build_query($params);
// build url
if ($this->port == null)
$full_url = $this->protocol . '://' . $this->server . $url;
else
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url;
error_log($full_url);
// curl
// curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_URL, $full_url);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 0);
$res = curl_exec($this->curl);
error_log($res);
}
protected function generateSignature($method, $url, $date_string)
{
$creds = [
$method,
$url,
$date_string,
$this->secret_key,
];
$sig_source = implode('|', $creds);
error_log('SIG SOURCE - ' . $sig_source);
$raw_sig = hash_hmac('sha1', $sig_source, $this->secret_key, true);
$enc_sig = base64_encode($raw_sig);
return $enc_sig;
}
protected function generateHeaders($method, $url, $date_string)
{
$sig = $this->generateSignature($method, $url, $date_string);
$headers = [
self::HEADER_API_KEY . ': ' . $this->api_key,
self::HEADER_SIGNATURE . ': ' . $sig,
self::HEADER_DATE . ': ' . $date_string,
];
return $headers;
}
}