Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Pest {
CURLOPT_RETURNTRANSFER => true, // return result instead of echoing
CURLOPT_SSL_VERIFYPEER => false, // stop cURL from verifying the peer's certificate
CURLOPT_FOLLOWLOCATION => false, // follow redirects, Location: headers
CURLOPT_MAXREDIRS => 10 // but dont redirect more than 10 times
CURLOPT_MAXREDIRS => 10, // but dont redirect more than 10 times
CURLOPT_HTTPHEADER => array() // the headers for http requests
);

public $base_url;
Expand Down Expand Up @@ -56,8 +57,24 @@ public function setupProxy($host, $port, $user = NULL, $pass = NULL) {
$this->curl_opts[CURLOPT_PROXYUSERPWD] = $user . ":" . $pass;
}
}

// Setup global headers
public function addHeader($header, $value) {
$this->curl_opts[CURLOPT_HTTPHEADER][$header] = $value;
}

public function getHeader($header) {
if(isset($this->curl_opts[CURLOPT_HTTPHEADER][$header])) {
return $this->curl_opts[CURLOPT_HTTPHEADER][$header];
}
return null;
}

public function removeHeader($header) {
unset($this->curl_opts[CURLOPT_HTTPHEADER][$header]);
}

public function get($url, $data=array()) {
public function get($url, $data=array(), $headers = array()) {
if (!empty($data)) {
$pos = strpos($url, '?');
if ($pos !== false) {
Expand All @@ -66,17 +83,21 @@ public function get($url, $data=array()) {
$url .= '?' . http_build_query($data);
}

$curl = $this->prepRequest($this->curl_opts, $url);
$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers);

$curl = $this->prepRequest($curl_opts, $url);
$body = $this->doRequest($curl);

$body = $this->processBody($body);

return $body;
}

public function head($url) {
public function head($url, $headers = array()) {
$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_NOBODY] = true;
$curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers);

$curl = $this->prepRequest($this->curl_opts, $url);
$body = $this->doRequest($curl);
Expand Down Expand Up @@ -109,7 +130,7 @@ public function post($url, $data, $headers=array()) {
$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'POST';
if (!is_array($data)) $headers[] = 'Content-Length: '.strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = array_merge($headers,$curl_opts[CURLOPT_HTTPHEADER]);
$curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers);
$curl_opts[CURLOPT_POSTFIELDS] = $data;

$curl = $this->prepRequest($curl_opts, $url);
Expand All @@ -126,7 +147,7 @@ public function put($url, $data, $headers=array()) {
$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
if (!is_array($data)) $headers[] = 'Content-Length: '.strlen($data);
$curl_opts[CURLOPT_HTTPHEADER] = $headers;
$curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers);
$curl_opts[CURLOPT_POSTFIELDS] = $data;

$curl = $this->prepRequest($curl_opts, $url);
Expand Down Expand Up @@ -154,9 +175,10 @@ public function patch($url, $data, $headers=array()) {
return $body;
}

public function delete($url) {
public function delete($url, $headers = array()) {
$curl_opts = $this->curl_opts;
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
$curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers);

$curl = $this->prepRequest($curl_opts, $url);
$body = $this->doRequest($curl);
Expand Down Expand Up @@ -233,6 +255,18 @@ private function handle_header($ch, $str) {
return strlen($str);
}

// Get global headers merged-in with local headers & preprocess them for curl usage
private function getHeaders($headers = array()) {
$headers = array_merge($this->curl_opts[CURLOPT_HTTPHEADER], $headers);
$headers = array_map(array($this, 'processRequestHeaders'), array_keys($headers), $headers);
return $headers;
}

// array mapper for headers
public function processRequestHeaders($key, $value) {
return $key . ': ' . $value;
}

private function doRequest($curl) {
$this->last_headers = array();

Expand Down