Skip to content

Commit

Permalink
this closes #110, add support for settings curl options
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill committed Apr 13, 2018
1 parent 0667f3d commit 46d2642
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/balances.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();
$api = new Binance\API( "/home/dave/.config/jaggedsoft/php-binance-api-1.json" );

// Get all of your positions, including estimated BTC value
$balances = $api->balances();
Expand Down
36 changes: 36 additions & 0 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class API {
protected $depthQueue = []; // /< Websockets depth queue
protected $chartQueue = []; // /< Websockets chart queue
protected $charts = []; // /< Websockets chart data
protected $curlOpts = []; // /< User defined curl coptions
protected $info = [
"timeOffset" => 0
]; // /< Additional connection options
Expand Down Expand Up @@ -97,6 +98,7 @@ private function __construct0() {
private function __construct1( string $filename = null ) {
$this->setupApiConfigFromFile( $filename );
$this->setupProxyConfigFromFile( $filename );
$this->setupCurlOptsFromFile( $filename );
}

/**
Expand Down Expand Up @@ -126,6 +128,9 @@ private function __construct3( string $api_key = null, string $api_secret = null
if( isset( $options[ 'useServerTime' ] ) && $options[ 'useServerTime' ] ) {
$this->useServerTime();
}
if( isset( $options[ 'curlOpts' ] ) && is_array( $options[ 'curlOpts' ] ) ) {
$this->curlOpts == $options[ 'curlOpts' ];
}
}

/**
Expand All @@ -144,8 +149,12 @@ private function __construct4( string $api_key = null, string $api_secret = null
if( isset( $options[ 'useServerTime' ] ) && $options[ 'useServerTime' ] ) {
$this->useServerTime();
}
if( isset( $options[ 'curlOpts' ] ) && is_array( $options[ 'curlOpts' ] ) ) {
$this->curlOpts == $options[ 'curlOpts' ];
}
$this->setupApiConfigFromFile();
$this->setupProxyConfigFromFile();
$this->setupCurlOptsFromFile();
}

/**
Expand All @@ -170,6 +179,27 @@ private function setupApiConfigFromFile( string $file = null ) {
$this->api_secret = isset( $contents[ 'api-secret' ] ) ? $contents[ 'api-secret' ] : "";
}

/**
* If no paramaters are supplied in the constructor, this function will attempt
* to load the acurlopts from the users home directory in the file
* ~/jaggedsoft/php-binance-api.json
*
* @param $file string file location
* @return null
*/
private function setupCurlOptsFromFile( string $file = null ) {
$file = is_null( $file ) ? getenv( "HOME" ) . "/.config/jaggedsoft/php-binance-api.json" : $file;

if( count( $this->curlOpts ) > 0 ) {
return;
}
if( file_exists( $file ) == false ) {
return;
}
$contents = json_decode( file_get_contents( $file ), true );
$this->curlOpts = isset( $contents[ 'curlOpts' ] ) && is_array( $contents[ 'curlOpts' ] ) ? $contents[ 'curlOpts' ] : [];
}

/**
* If no paramaters are supplied in the constructor for the proxy confguration,
* this function will attempt to load the proxy info from the users home directory
Expand Down Expand Up @@ -828,6 +858,12 @@ private function httpRequest( string $url, string $method = "GET", array $params
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );

// set user defined curl opts last for overriding
foreach( $this->curlOpts as $key => $value ) {
curl_setopt( $ch, constant( $key ), $value );
}

$output = curl_exec( $ch );
// Check if any error occurred
if( curl_errno( $ch ) > 0 ) {
Expand Down

0 comments on commit 46d2642

Please sign in to comment.