From 029ad4e26e7d29aab7abae48bb40a071d3a1dd77 Mon Sep 17 00:00:00 2001 From: Omar Gonzalez Date: Sat, 21 Apr 2012 21:14:10 -0700 Subject: [PATCH 1/2] New addHeader() method added -Added an addHeader() method that lets you add a header value to the Pest object before doing a get() for example. --- Pest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Pest.php b/Pest.php index a59b48b..f04516c 100644 --- a/Pest.php +++ b/Pest.php @@ -40,6 +40,16 @@ public function __construct($base_url) { // Using PHP 5.2, it cannot be initialised in the static context $this->curl_opts[CURLOPT_HEADERFUNCTION] = array($this, 'handle_header'); } + + // Lets you add a header to the header list + // Example: pest->addHeader("Accept", "application/xml"); + public function addHeader($headerName, $value) { + if (is_array($this->curl_opts[CURLOPT_HTTPHEADER])) { + array_push($this->curl_opts[CURLOPT_HTTPHEADER], $headerName.":".$value); + } else { + $this->curl_opts[CURLOPT_HTTPHEADER] = array($headerName.":".$value); + } + } // $auth can be 'basic' or 'digest' public function setupAuth($user, $pass, $auth = 'basic') { From e1c7ada1b20ce7005f279cfd9a5f8453243f998b Mon Sep 17 00:00:00 2001 From: Omar Gonzalez Date: Sat, 21 Apr 2012 22:59:08 -0700 Subject: [PATCH 2/2] Updated the addHeader() to use a try/catch block. The is_array() line was crashing if CURLOPT_HTTPHEADER didn't exist. Using the try/catch block fixed the error. --- Pest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pest.php b/Pest.php index f04516c..55261e1 100644 --- a/Pest.php +++ b/Pest.php @@ -44,9 +44,9 @@ public function __construct($base_url) { // Lets you add a header to the header list // Example: pest->addHeader("Accept", "application/xml"); public function addHeader($headerName, $value) { - if (is_array($this->curl_opts[CURLOPT_HTTPHEADER])) { + try { array_push($this->curl_opts[CURLOPT_HTTPHEADER], $headerName.":".$value); - } else { + } catch (Exception $e) { $this->curl_opts[CURLOPT_HTTPHEADER] = array($headerName.":".$value); } }