Skip to content

Instantly share code, notes, and snippets.

@5baddi
Last active December 23, 2019 09:45
Show Gist options
  • Save 5baddi/d8c8c7f42ec74ffad6f114ab0ea3dec4 to your computer and use it in GitHub Desktop.
Save 5baddi/d8c8c7f42ec74ffad6f114ab0ea3dec4 to your computer and use it in GitHub Desktop.
<?php
namespace BADDI\SMP;
/**
* cURLHelper class
*/
class cURLHelper
{
/** Default cURL options */
private const DEFAULT_CURL_OPTIONS = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
CURLOPT_SSL_VERIFYPEER => true,
];
/** Allowed HTTP method for CRUD */
const ALLOWED_HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE'];
/**
* cURL ressource
*/
private $handler;
/**
* Target URL
*
* @var string
*/
public $url;
/**
* Custom cURL options
*
* @var array
*/
public $options;
/**
* HTTP status code
*
* @var int
*/
public $httpStatusCode;
/**
* cURL response
*/
public $response;
/**
* Constructor
*
* @throws Exception
* @return BADDI\SMP\cURL
*/
public function __construct()
{
// Verify exists cURL extension
if(!extension_loaded('curl'))
throw new \Exception('Could not load cURl extension or is missing.');
// Init cURL
$this->handler = curl_init();
return $this;
}
/**
* Set HTTP options
*
* @var array $options
* @return BADDI\SMP\cURL
*/
public function setOptions($options = self::DEFAULT_CURL_OPTIONS)
{
// Set cURL options
curl_setopt_array($this->handler, ($this->options = $options));
return $this;
}
/**
* Set URL
*
* @param string $URL
* @return BADDI\SMP\cURL
*/
public function setURL(string $URL)
{
// Set/Replace URL
curl_setopt($this->handler, CURLOPT_URL, ($this->url = $URL));
return $this;
}
/**
* Send cURL request
*
* @param array $params
* @param string $uriRoute
* @param string $method
* @param array $options
* @throws Exception
* @return mixed
*/
public function send(array $params, string $uriRoute = null, string $method = 'GET', array $options = null)
{
// Verify allowed HTTP method
$method = strtoupper($method);
if(!in_array($method, self::ALLOWED_HTTP_METHODS))
throw new \Exception($method . " method has not allowed.");
// Reset all cURL options
curl_reset($this->handler);
// Set cURL options
if(!is_null($options))
curl_setopt_array($this->handler, ($this->options = $options));
else
curl_setopt_array($this->handler, (!is_null($this->options) ? $this->options : self::DEFAULT_CURL_OPTIONS));
// Parse and set the URL with targted route
if(!is_null($uriRoute))
curl_setopt($this->handler, CURLOPT_URL, preg_replace('/(\/+)/', '/', $this->url . $uriRoute));
// Set GET or POST method
if($method == 'GET')
curl_setopt($this->handler, CURLOPT_HTTPGET, true);
elseif($method == 'POST')
curl_setopt($this->handler, CURLOPT_POST, true);
else
curl_setopt($this->handler, CURLOPT_CUSTOMREQUEST, $method);
// Execute the cURl request
$response = curl_exec($this->handler);
$json = json_decode($response, true);
$this->response = (!is_null($json) ? $json : $response);
// Get HTTP status code
$this->httpStatusCode = curl_getinfo($this->handler, CURLINFO_HTTP_CODE);
// Handle empty response
if(is_null($this->response))
throw new \Exception('The request returned an empty response. Code: ' . curl_errno($this->handler) . 'Message: ' . curl_error($this->handler));
return $this->response;
}
/**
* End cURL process
*
* @return void
*/
public function end() : void
{
// Close cURL
if(is_resource($this->handler))
curl_close($this->handler);
$this->handler = null;
}
}
@5baddi
Copy link
Author

5baddi commented Dec 2, 2019

cURL Helper to make cURL request easy using OOP structure.

@shymhd
Copy link

shymhd commented Dec 2, 2019

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment