From 91bd27cdf854ce60b6389dcbfe6147bc9b6d07d5 Mon Sep 17 00:00:00 2001 From: Patrick Tolvstein Date: Mon, 15 Feb 2016 12:52:31 +0100 Subject: [PATCH] Add Client::set_headers with the possibility to set additional custom headers. Used in Request::execute to possibly set the Content-Length of the JSON request body when sending form data Also added the header Content-Type to align the request headers with the requirements of the QuickPay API docs --- QuickPay/API/Client.php | 110 ++++++++++++++++++++++----------------- QuickPay/API/Request.php | 12 ++++- 2 files changed, 72 insertions(+), 50 deletions(-) diff --git a/QuickPay/API/Client.php b/QuickPay/API/Client.php index 7c45452..d416329 100644 --- a/QuickPay/API/Client.php +++ b/QuickPay/API/Client.php @@ -1,57 +1,56 @@ auth_string = $auth_string; - + // Instantiate cURL object $this->authenticate(); } - - /** - * shutdown function. - * - * Closes the current cURL connection - * - * @access public - */ + + /** + * shutdown function. + * + * Closes the current cURL connection + * + * @access public + */ public function shutdown() { if( ! empty( $this->ch ) ) @@ -59,36 +58,49 @@ public function shutdown() curl_close( $this->ch ); } } - - - /** - * authenticate function. - * - * Create a cURL instance with authentication headers - * - * @access public - */ + + + /** + * authenticate function. + * + * Create a cURL instance with authentication headers + * + * @access public + */ protected function authenticate() { $this->ch = curl_init(); - + + $options = array( + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC + ); + + curl_setopt_array( $this->ch, $options ); + } + + public function set_headers( $additional_headers = array() ) + { $headers = array( 'Accept-Version: v10', - 'Accept: application/json', + 'Accept: application/json', + 'Content-Type: application/json', ); - if( ! empty($this->auth_string)) + if( ! empty($this->auth_string)) { $headers[] = 'Authorization: Basic ' . base64_encode( $this->auth_string ); } - $options = array( - CURLOPT_RETURNTRANSFER => true, - CURLOPT_SSL_VERIFYPEER => true, - CURLOPT_HTTPAUTH => CURLAUTH_BASIC, - CURLOPT_HTTPHEADER => $headers - ); - - curl_setopt_array( $this->ch, $options ); + if( ! empty( $additional_headers ) ) + { + foreach( $additional_headers as $additional_header ) + { + $headers[] = $additional_header; + } + } + + curl_setopt( $this->ch, CURLOPT_HTTPHEADER, $headers); } } diff --git a/QuickPay/API/Request.php b/QuickPay/API/Request.php index faf7ce7..44a87fb 100644 --- a/QuickPay/API/Request.php +++ b/QuickPay/API/Request.php @@ -171,7 +171,17 @@ protected function execute( $request_type, $form = array() ) // If additional data is delivered, we will send it along with the API request if( is_array( $form ) && ! empty( $form ) ) { - curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form) ); + $json_data = json_encode( $form ); + curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, $json_data ); + $this->client->set_headers( + array( + 'Content-Length: ' . strlen($json_data) + ) + ); + } + else + { + $this->client->set_headers(); } // Store received headers in temporary memory file, remember sent headers