Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\Uri\Uri;
use Joomla\CMS\Http\HttpFactory;
// API Script - GET and POST requests using Curl or Joomla Http
// For Testing: https://reqres.in/
class TApi
{
public $ch;
// Request
public $url;
public $headers = []; // key-value
public $curl_headers = [];
// Response
public $response;
public $curl_info;
public $code;
public $response_headers = [];
public $error;
public $content_type;
public $accept;
public function __construct($url = '', $headers = [], $curl_options = [])
{
// 4 Aspects: Url to send request, Headers, Data, Curl Options
// $headers - array of key-value pairs
$this->ch = curl_init();
$this->url = $url;
$this->headers = $headers;
foreach ($headers as $k => $v)
{
if ($k == 'Content-Type')
{
$this->content_type = $v;
}
if ($k == 'Accept')
{
$this->accept = $v;
}
$this->curl_headers[] = $k . ': ' . $v;
}
curl_setopt($this->ch, CURLOPT_URL, $this->url);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->curl_headers);
foreach ($curl_options as $k => $v)
{
curl_setopt($this->ch, $k, $v);
}
}
public function send_request($method = 'GET', $data = null)
{
// $data - array
switch (strtoupper($method))
{
case 'GET':
// POST data not required
break;
case 'POST':
$this->set_post_data($data);
break;
case 'PUT':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$this->set_post_data($data);
break;
case 'DELETE':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
// Execute the request
$this->response = curl_exec($this->ch);
if (curl_errno($this->ch))
{
$this->error = curl_error($this->ch);
$this->error = curl_errno($this->ch);
}
// url, content_type, http_code, header_size
$this->curl_info = curl_getinfo($this->ch);
$this->code = $this->curl_info['http_code'];
// Split header and body
$header_text = substr($this->response, 0, $this->curl_info['header_size']);
$body = substr($this->response, $this->curl_info['header_size']);
curl_close($this->ch);
foreach (explode("\r\n", trim($header_text)) as $line)
{
if (strpos($line, ':') != false)
{
[$key, $value] = explode(': ', $line, 2);
$this->response_headers[$key] = $value;
}
}
// use Content-Type of response headers to identify
$body = ($this->accept == 'application/json') ? json_decode($body, true) : $body;
return $body;
}
// Set POST data
public function set_post_data($data = [])
{
// $data - array with key-value pairs
if ($this->content_type == 'application/json')
{
$post_data = json_encode($data);
}
elseif ($this->content_type == 'application/x-www-form-urlencoded')
{
$post_data = http_build_query($data);
}
else
{
$post_data = $data;
}
curl_setopt($this->ch, CURLOPT_POST, true);
if ($post_data)
{
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_data);
}
}
// GET Request using Joomla Http
public function curl_get()
{
$http = HttpFactory::getHttp();
$http->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Response code, body, headers
$this->response = $http->get($this->url, $this->headers);
$this->code = $this->response->code;
$response = ($this->accept == 'application/json') ? json_decode($this->response->body, true) : $this->response->body;
return $response;
// headers - Content-Type, Content-Length, Server, Date, Cache-Control, Location, Set-Cookie, Content-Disposition
}
// POST Request using Joomla Http
public function curl_post($data = [])
{
if ($this->content_type == 'application/json')
{
$post_data = json_encode($data);
}
elseif ($this->content_type == 'application/x-www-form-urlencoded')
{
$post_data = http_build_query($data);
}
else
{
$post_data = $data;
}
$http = HttpFactory::getHttp();
$http->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$this->response = $http->post($this->url, $post_data, $this->headers);
$this->code = $this->response->code;
$response = ($this->accept == 'application/json') ? json_decode($this->response->body, true) : $this->response->body;
return $response;
}
// DELETE Request using Joomla Http
public function curl_delete()
{
$http = HttpFactory::getHttp();
$this->response = $http->delete($this->url, $this->headers);
$this->code = $this->response->code;
$response = ($this->accept == 'application/json') ? json_decode($this->response->body, true) : $this->response->body;
return $response;
}
public function get_error()
{
return $this->error;
}
// Get the error code
public function get_code()
{
return $this->code;
}
// Create URL for OAuth2 authentication
public function create_url($options = [])
{
if (!$options['authurl'] || !$options['client_id'])
{
Factory::getApplication()->enqueueMessage('Authorization URL and client_id are required');
return false;
}
$url = new Uri($options['authurl']);
$url->setVar('response_type', 'code');
$url->setVar('client_id', urlencode($options['client_id']));
if ($redirect = $options['redirect_uri'])
{
$url->setVar('redirect_uri', urlencode($redirect));
}
if ($scope = $options['scope'])
{
$scope = \is_array($scope) ? implode(' ', $scope) : $scope;
$url->setVar('scope', urlencode($scope));
}
if ($state = $options['state'])
{
$url->setVar('state', urlencode($state));
}
if (\is_array($options['requestparams']))
{
foreach ($options['requestparams'] as $key => $value)
{
$url->setVar($key, urlencode($value));
}
}
return (string) $url;
}
public function get_access_token($options = [], $code = null)
{
if (!$code)
{
$code = Factory::getApplication()->input->get('code', false, 'raw');
}
$data = array(
'code' => $code,
'client_id' => $options['client_id'],
'client_secret' => $options['client_secret'],
'redirect_uri' => $options['redirect_uri'],
'grant_type' => 'authorization_code',
);
$http = HttpFactory::getHttp();
$response = $http->post($options['tokenurl'], $data, $options['headers']);
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
/*
if (strpos($response->headers['Content-Type'], 'application/json') !== false)
{
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
}
else
{
parse_str($response->body, $token);
$token = array_merge($token, ['created' => time()]);
}
*/
return $token;
}
// Set error message
public function set_error($message)
{
if (\is_array($message))
{
$message = json_encode($message);
}
Factory::getApplication()->enqueueMessage($message, 'error');
}
}