From 3a851c8fadba3e785532fff4b0489f5c434495f2 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Sun, 1 Nov 2015 21:50:04 +0000 Subject: [PATCH 1/5] CURLOPT_SSL_VERIFYHOST is an integer (0, 1, 2) not a boolean. --- lib/woocommerce-api/class-wc-api-client-http-request.php | 3 ++- lib/woocommerce-api/class-wc-api-client.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/woocommerce-api/class-wc-api-client-http-request.php b/lib/woocommerce-api/class-wc-api-client-http-request.php index d6684fa..26bc8f9 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -61,7 +61,8 @@ public function __construct( $args ) { // optional cURL opts $timeout = (int) $args['options']['timeout']; - $ssl_verify = (bool) $args['options']['ssl_verify']; + // 0, 1 or 2. Value 1 is deprecated. + $ssl_verify = (int) $args['options']['ssl_verify']; $this->ch = curl_init(); diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index 030109b..3fbcbad 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -35,7 +35,7 @@ class WC_API_Client { public $timeout = 30; /** @var bool true to perform SSL peer verification */ - public $ssl_verify = true; + public $ssl_verify = 2; /** Resources */ From 532c5e8d919dce1c2090130aec63205162ec0c87 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Sun, 1 Nov 2015 21:55:34 +0000 Subject: [PATCH 2/5] Better way of handling CURLOPT_SSL_VERIFYHOST - map true/false to 0/2 --- lib/woocommerce-api/class-wc-api-client-http-request.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/woocommerce-api/class-wc-api-client-http-request.php b/lib/woocommerce-api/class-wc-api-client-http-request.php index 26bc8f9..1eb183f 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -61,14 +61,14 @@ public function __construct( $args ) { // optional cURL opts $timeout = (int) $args['options']['timeout']; - // 0, 1 or 2. Value 1 is deprecated. - $ssl_verify = (int) $args['options']['ssl_verify']; + $ssl_verify = (bool) $args['options']['ssl_verify']; $this->ch = curl_init(); // default cURL opts curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); - curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify ); + // 0, 1 or 2. Value 1 is deprecated. + curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify ? 0 : 2 ); curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt( $this->ch, CURLOPT_TIMEOUT, (int) $timeout ); curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true ); From ef3eded6c62e3dc70403dff73f42def687e73f4b Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Mon, 2 Nov 2015 20:47:59 +0000 Subject: [PATCH 3/5] Issue #134 --- example/example.php | 1 + .../class-wc-api-client-http-request.php | 15 ++++++++++++--- lib/woocommerce-api/class-wc-api-client.php | 6 ++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/example/example.php b/example/example.php index 3197bda..3a14568 100644 --- a/example/example.php +++ b/example/example.php @@ -8,6 +8,7 @@ 'validate_url' => false, 'timeout' => 30, 'ssl_verify' => false, + 'fix_method' => false, ); try { diff --git a/lib/woocommerce-api/class-wc-api-client-http-request.php b/lib/woocommerce-api/class-wc-api-client-http-request.php index 1eb183f..5b758b8 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -69,7 +69,7 @@ public function __construct( $args ) { curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); // 0, 1 or 2. Value 1 is deprecated. curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify ? 0 : 2 ); - curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, $timeout ); + curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout ); curl_setopt( $this->ch, CURLOPT_TIMEOUT, (int) $timeout ); curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true ); @@ -79,8 +79,18 @@ public function __construct( $args ) { // save response headers curl_setopt( $this->ch, CURLOPT_HEADERFUNCTION, array( $this, 'curl_stream_headers' ) ); + // start with the required REST method + $method = $this->request->method; + + // if we want to fix the method to GET and POST, then move the REST method + // to the _method parameter and do a POST instead. + if ( $args['fix_method'] && $method != 'GET' && $method != 'POST' ) { + $this->request->params = array_merge( $this->request->params, array( '_method' => $method ) ); + $method = 'POST'; + } + // set request method and data - switch ( $this->request->method ) { + switch ( $method ) { case 'GET': $this->request->body = null; @@ -179,7 +189,6 @@ public function dispatch() { // check for invalid JSON if ( null === $parsed_response ) { - throw new WC_API_Client_HTTP_Exception( sprintf( 'Invalid JSON returned for %s.', $this->request->url ), $this->response->code, $this->request, $this->response ); } diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index 3fbcbad..dd2634b 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -37,6 +37,9 @@ class WC_API_Client { /** @var bool true to perform SSL peer verification */ public $ssl_verify = 2; + /** @var bool true to replace PUT and DELETE methods with a POST */ + public $fix_method = false; + /** Resources */ /** @var WC_API_Client_Resource_Coupons instance */ @@ -182,6 +185,7 @@ public function parse_options( $options ) { 'validate_url', 'timeout', 'ssl_verify', + 'fix_method', ); foreach ( (array) $options as $opt_key => $opt_value ) { @@ -197,6 +201,7 @@ public function parse_options( $options ) { $this->$opt_key = $opt_value; } + } @@ -260,6 +265,7 @@ public function make_api_call( $method, $path, $request_data ) { $args = array( 'method' => $method, + 'fix_method' => $this->fix_method, 'url' => $this->api_url . $path, 'data' => $request_data, 'consumer_key' => $this->consumer_key, From 21bb09a09fc98eb76b7af0a169518c9e2a45e4c2 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Wed, 4 Nov 2015 10:42:44 +0000 Subject: [PATCH 4/5] Issue #134 Add some notes on how and when to use in the README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 87d0bba..2f7392c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ require_once( 'lib/woocommerce-api.php' ); $options = array( 'ssl_verify' => false, + // If your server will only accept GET and POST, then try this fix + // to map PUT and DELETE to POST. + 'fix_method' => true, ); try { From 6363b9c8640d6dedbfcb181afe3fb1ed2eae409c Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Sat, 14 Nov 2015 10:26:04 +0000 Subject: [PATCH 5/5] $ssl_verify is still a boolean; the mapping to 0/2 is done later. --- lib/woocommerce-api/class-wc-api-client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index dd2634b..7a03b3c 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -35,7 +35,7 @@ class WC_API_Client { public $timeout = 30; /** @var bool true to perform SSL peer verification */ - public $ssl_verify = 2; + public $ssl_verify = true; /** @var bool true to replace PUT and DELETE methods with a POST */ public $fix_method = false;