Skip to content
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
4 changes: 4 additions & 0 deletions lib/woocommerce-api/class-wc-api-client-http-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function __construct( $args ) {

$this->ch = curl_init();

if($args['options']['httpauth_username'] !== null) {
curl_setopt($this->ch, CURLOPT_USERPWD, $args['options']['httpauth_username'] . ":" . $args['options']['httpauth_password']);
}

// default cURL opts
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
Expand Down
22 changes: 21 additions & 1 deletion lib/woocommerce-api/class-wc-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class WC_API_Client {
/** @var bool true to perform SSL peer verification */
public $ssl_verify = true;

/** @var null|string if set, used for HTTP BASIC AUTH */
public $httpauth_username = null;

/** @var null|string if set, used for HTTP BASIC AUTH */
public $httpauth_password = null;

/** Resources */

/** @var WC_API_Client_Resource_Coupons instance */
Expand Down Expand Up @@ -191,6 +197,8 @@ public function parse_options( $options ) {
'validate_url',
'timeout',
'ssl_verify',
'httpauth_username',
'httpauth_password',
);

foreach ( (array) $options as $opt_key => $opt_value ) {
Expand Down Expand Up @@ -218,7 +226,17 @@ public function parse_options( $options ) {
*/
public function validate_api_url() {

$index = @file_get_contents( $this->api_url );
if($this->httpauth_username !== null) {
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode($this->httpauth_username.':'.$this->httpauth_password)
)
));

$index = @file_get_contents( $this->api_url, false, $context);
} else {
$index = @file_get_contents( $this->api_url);
}

// check for HTTP 404 response (file_get_contents() returns false when encountering 404)
// this usually means:
Expand Down Expand Up @@ -278,6 +296,8 @@ public function make_api_call( $method, $path, $request_data ) {
'ssl_verify' => $this->ssl_verify,
'json_decode' => $this->return_as_array ? 'array' : 'object',
'debug' => $this->debug,
'httpauth_username' => $this->httpauth_username,
'httpauth_password' => $this->httpauth_password,
)
);

Expand Down