Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support option for servers that don't accept PUT or DELETE methods #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
'fix_method' => false,
);

try {
Expand Down
18 changes: 14 additions & 4 deletions lib/woocommerce-api/class-wc-api-client-http-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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;
Expand Down Expand Up @@ -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 );
}

Expand Down
6 changes: 6 additions & 0 deletions lib/woocommerce-api/class-wc-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -182,6 +185,7 @@ public function parse_options( $options ) {
'validate_url',
'timeout',
'ssl_verify',
'fix_method',
);

foreach ( (array) $options as $opt_key => $opt_value ) {
Expand All @@ -197,6 +201,7 @@ public function parse_options( $options ) {

$this->$opt_key = $opt_value;
}

}


Expand Down Expand Up @@ -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,
Expand Down