From 8a0ba36360b90ebd2d53f535271449ebb893d6cf Mon Sep 17 00:00:00 2001 From: Pavel Ptacek Date: Fri, 26 Apr 2013 02:56:16 +0200 Subject: [PATCH 1/3] Allow headers to be submitted to GET / function --- Pest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Pest.php b/Pest.php index b2ab978..1b8c99a 100644 --- a/Pest.php +++ b/Pest.php @@ -57,7 +57,7 @@ public function setupProxy($host, $port, $user = NULL, $pass = NULL) { } } - public function get($url, $data=array()) { + public function get($url, $data=array(), array $headers = array()) { if (!empty($data)) { $pos = strpos($url, '?'); if ($pos !== false) { @@ -66,7 +66,13 @@ public function get($url, $data=array()) { $url .= '?' . http_build_query($data); } - $curl = $this->prepRequest($this->curl_opts, $url); + $curl_opts = $this->curl_opts; + if($headers) { + $curl_opts[CURLOPT_HTTPHEADER] = array(); + $curl_opts[CURLOPT_HTTPHEADER] = array_map(function($key, $value) { return $key . ': ' . $value; }, array_keys($headers), $headers); + } + + $curl = $this->prepRequest($curl_opts, $url); $body = $this->doRequest($curl); $body = $this->processBody($body); From dcf5bb076ae039208e6f375455be27498e979c80 Mon Sep 17 00:00:00 2001 From: Pavel Ptacek Date: Fri, 26 Apr 2013 03:22:25 +0200 Subject: [PATCH 2/3] rewritten the headers functions to work globally & locally. --- Pest.php | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Pest.php b/Pest.php index 1b8c99a..08b771d 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(), array $headers = array()) { + public function get($url, $data=array(), $headers = array()) { if (!empty($data)) { $pos = strpos($url, '?'); if ($pos !== false) { @@ -67,10 +84,7 @@ public function get($url, $data=array(), array $headers = array()) { } $curl_opts = $this->curl_opts; - if($headers) { - $curl_opts[CURLOPT_HTTPHEADER] = array(); - $curl_opts[CURLOPT_HTTPHEADER] = array_map(function($key, $value) { return $key . ': ' . $value; }, array_keys($headers), $headers); - } + $curl_opts[CURLOPT_HTTPHEADER] = $this->getHeaders($headers); $curl = $this->prepRequest($curl_opts, $url); $body = $this->doRequest($curl); @@ -80,9 +94,10 @@ public function get($url, $data=array(), array $headers = 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); @@ -115,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); @@ -132,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); @@ -239,6 +254,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(); From 662c66a867ddeeee84d8f54bc8c49373334b1c38 Mon Sep 17 00:00:00 2001 From: "Michael J. Flynn" Date: Fri, 7 Jun 2013 19:15:26 -0400 Subject: [PATCH 3/3] Added curl http headers to the delete function --- Pest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Pest.php b/Pest.php index 08b771d..a851cc6 100644 --- a/Pest.php +++ b/Pest.php @@ -175,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);