Skip to content

Commit

Permalink
add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ReactMVC authored Oct 1, 2023
1 parent f66ee8a commit 7c3b847
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions src/HTTPMonster.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ public function __construct()
$this->setDefaults();
}

/**
* Header - adds a single HTTP header to the request
*
* @param string $header - the header to add
* @return HTTPMonster - returns the HTTPMonster object for chaining
*/
public function Header($header)
{
$this->options[CURLOPT_HTTPHEADER][] = $header;
return $this;
}

/**
* Headers - sets the HTTP headers for the request
*
Expand Down Expand Up @@ -120,6 +108,27 @@ public function Send()
return $response;
}

/**
* getHeaders - returns the response headers
*
* @return array - the response headers
*/
public function getHeaders()
{
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_NOBODY, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$headers) {
$trimmedHeader = trim($header);
if (!empty($trimmedHeader)) {
$headers[] = $trimmedHeader;
}
return strlen($header);
});
curl_exec($this->ch);
return $headers ?? [];
}

/**
* getStatus - returns the HTTP status code of the response
*
Expand All @@ -130,6 +139,58 @@ public function getStatus()
return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
}

/**
* Encoding - sets the encoding(s) for the request
*
* @param string|array $encodings - the encoding(s) to set
* @return HTTPMonster - returns the HTTPMonster object for chaining
*/
public function Encoding($encodings)
{
if (is_array($encodings)) {
$encodings = implode(',', $encodings);
}
$this->Option(CURLOPT_ENCODING, $encodings);
return $this;
}

/**
* MaxRedirects - sets the maximum number of redirects to follow
*
* @param int $maxRedirects - the maximum number of redirects
* @return HTTPMonster - returns the HTTPMonster object for chaining
*/
public function MaxRedirects($maxRedirects)
{
$this->Option(CURLOPT_MAXREDIRS, $maxRedirects);
return $this;
}

/**
* VerifyPeer - sets whether to verify the peer's SSL certificate
*
* @param bool $verify - whether to verify the peer's SSL certificate
* @return HTTPMonster - returns the HTTPMonster object for chaining
*/
public function VerifyPeer($verify)
{
$this->Option(CURLOPT_SSL_VERIFYPEER, $verify);
return $this;
}

/**
* Proxy - sets the proxy for the request
*
* @param string $proxy - the proxy to set
* @return HTTPMonster - returns the HTTPMonster object for chaining
*/
public function Proxy($proxy)
{
$this->Option(CURLOPT_PROXY, $proxy);
return $this;
}


/**
* setDefaults - sets some default cURL options for the request
*
Expand Down

0 comments on commit 7c3b847

Please sign in to comment.