Skip to content

Commit

Permalink
Added timeout option. Fixed code doc formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrarieben authored Feb 11, 2024
1 parent 2a4b90e commit d638e47
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions lib/classes/ApiCall.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace ezrarieben;

/**
* This class is used to make a JSON REST API call
*
* @var $curl --> The cURL session
* @var $result --> API Result
* @var $responseCode --> HTTP response code
*/
* This class is used to make a JSON REST API call
*
* @var $curl The cURL session
* @var $result API result
* @var $responseCode HTTP response code
*/
class ApiCall
{
private $curl;
Expand All @@ -17,15 +17,14 @@ class ApiCall
private $responseCode;

/**
* Constructor
*
* @param string $url --> URL to call
* @param array $data --> Data to send to API via POST
* @param bool $dataViaGET --> Send the data via GET instead? (default: false)
*
* @return ApiCall
*/
public function __construct(string $url = '', array $data = array(), bool $dataViaGET = false)
* Constructor
*
* @param string $url URL to call
* @param array $data Data to send to API
* @param array $dataViaGET Send the data via GET instead? (default: false)
* @param int $timeout Amount of seconds to wait for CURL response before timeout (default: 0)
*/
public function __construct(string $url = '', array $data = array(), bool $dataViaGET = false, int $timeout = 0)
{
$this->curl = curl_init();

Expand All @@ -39,12 +38,13 @@ public function __construct(string $url = '', array $data = array(), bool $dataV
curl_setopt($this->curl, CURLOPT_URL, "{$url}?{$query}");
}
} else {
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_URL, $url);
}

curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);

// Get API response
$this->response = curl_exec($this->curl);
Expand All @@ -54,11 +54,11 @@ public function __construct(string $url = '', array $data = array(), bool $dataV
}

/**
* Getter function for API request result
*
* @return string --> API Response
*/
public function getResponse(): String
* Getter function for API request result
*
* @return string API Response
*/
public function getResponse(): string
{
// Remove header from response
$headerSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
Expand All @@ -68,32 +68,32 @@ public function getResponse(): String
}

/**
* Getter function for API HTML response code
*
* @return int --> HTML response code as int
*/
* Getter function for API HTML response code
*
* @return int HTML response code as int
*/
public function getResponseCode(): int
{
return $this->responseCode;
}

/**
* This function gets the header from the cURL request
* NOTE: Requires 'CURLOPT_HEADER' to be set to true
* Thanks to: Markus Knappen Johansson on StackOverflow --> https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl
*
* @param $ch --> cURL call reference
* @param $response --> Response from curl_exec()
*
* @return array --> Array of headers
*/
* This function gets the header from the cURL request
* NOTE: Requires 'CURLOPT_HEADER' to be set to true
* Thanks to: Markus Knappen Johansson on StackOverflow --> https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl
*
* @param $ch cURL call reference
* @param $response Response from curl_exec()
*
* @return array Array of headers
*/
public function getHeader(): array
{
$headerSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$header = substr($this->response, 0, $headerSize);
$headers = array();
$arrRequests = explode("\r\n\r\n", $header);
for ($index = 0; $index < count($arrRequests) -1; $index++) {
for ($index = 0; $index < count($arrRequests) - 1; $index++) {
foreach (explode("\r\n", $arrRequests[$index]) as $i => $line) {
if ($i === 0) {
$headers[$index]['http_code'] = $line;
Expand All @@ -108,11 +108,11 @@ public function getHeader(): array
}

/**
* This function closes the open cURL session
*
* @return Void
*/
public function close(): Void
* This function closes the open cURL session
*
* @return void
*/
public function close(): void
{
curl_close($this->curl);
}
Expand Down

0 comments on commit d638e47

Please sign in to comment.