diff --git a/Pest.php b/Pest.php index b2ab978..a851cc6 100644 --- a/Pest.php +++ b/Pest.php @@ -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; @@ -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) { @@ -66,7 +83,10 @@ 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); @@ -74,9 +94,10 @@ public function get($url, $data=array()) { 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); @@ -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); @@ -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); @@ -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); @@ -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();