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 { 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 d6684fa..5b758b8 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -67,8 +67,9 @@ public function __construct( $args ) { // default cURL opts curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); - curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify ); - curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, $timeout ); + // 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, (int) $timeout ); curl_setopt( $this->ch, CURLOPT_TIMEOUT, (int) $timeout ); curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true ); @@ -78,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; @@ -178,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 030109b..7a03b3c 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 = true; + /** @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,