diff --git a/README.md b/README.md index ed53b96..9bd858a 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,22 @@ include_once './vendor/autoload.php'; ## API Documentation ## API documentation generated from phpDocs by apigen is available in the docs/ folder. +## Using a Proxy ## +You can configure the library to use a proxy if needed for debugging or otherwise. I've used it with [Charles Proxy]() many times to debug my calls and responses from ApiAxle. To configure it to use a proxy, simply set these attributes in the configuration: +```php + 'proxy_enable' => true, + 'proxy_host' => '127.0.0.1', + 'proxy_port' => '8888', +``` + +## Using your own CA Certs ## +If you are running in an environment where you have your own CA and want to validate the certs, you can configure this library to specify where to find the CA info or a path to a folder with certs. This uses the curl_setopt features in PHP to set these. In order for them to work though, the ssl_verifypeer setting needs to be ```true```: +```php + 'ssl_verifypeer' => true, + 'ssl_cainfo' => null, + 'ssl_capath' => '/etc/pki/tls/certs/', +``` + ## Contributing ## If you are interested in contributing to this library and/or extending it please let me know, I'd love to work with others on this to help consider other use cases and design patterns. diff --git a/config/config.local.php.dist b/config/config.local.php.dist index 765b888..82ec6cc 100644 --- a/config/config.local.php.dist +++ b/config/config.local.php.dist @@ -5,6 +5,8 @@ return array( 'key' => 'somerandomstring', 'secret' => 'somelongerrandomstring', 'ssl_verifypeer' => true, + 'ssl_cainfo' => null, + 'ssl_capath' => null, 'proxy_enable' => false, 'proxy_host' => '127.0.0.1', 'proxy_port' => '8888', diff --git a/config/config.travis.php b/config/config.travis.php index f385b22..ec72a23 100644 --- a/config/config.travis.php +++ b/config/config.travis.php @@ -5,6 +5,8 @@ 'key' => 'apiaxle-travis-ci-key', 'secret' => 'apiaxle-travis-ci-secret', 'ssl_verifypeer' => true, + 'ssl_cainfo' => null, + 'ssl_capath' => null, 'proxy_enable' => false, 'proxy_host' => '127.0.0.1', 'proxy_port' => '8888', diff --git a/docs/class-ApiAxle.Api.Api.html b/docs/class-ApiAxle.Api.Api.html index c4dc1c3..a2b70bc 100644 --- a/docs/class-ApiAxle.Api.Api.html +++ b/docs/class-ApiAxle.Api.Api.html @@ -121,7 +121,7 @@

Class Api

Author: Phillip Shipley <phillip@phillipshipley.com>
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
Located at src/ApiAxle/Api/Api.php
diff --git a/docs/class-ApiAxle.Api.Key.html b/docs/class-ApiAxle.Api.Key.html index a5b5764..31b2a28 100644 --- a/docs/class-ApiAxle.Api.Key.html +++ b/docs/class-ApiAxle.Api.Key.html @@ -121,7 +121,7 @@

Class Key

Author: Phillip Shipley <phillip@phillipshipley.com>
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
Located at src/ApiAxle/Api/Key.php
diff --git a/docs/class-ApiAxle.Api.Keyring.html b/docs/class-ApiAxle.Api.Keyring.html index 456316e..2abdd27 100644 --- a/docs/class-ApiAxle.Api.Keyring.html +++ b/docs/class-ApiAxle.Api.Keyring.html @@ -121,7 +121,7 @@

Class Keyring

Author: Phillip Shipley <phillip@phillipshipley.com>
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
Located at src/ApiAxle/Api/Keyring.php
diff --git a/docs/class-ApiAxle.Shared.Config.html b/docs/class-ApiAxle.Shared.Config.html index 3721274..5dffd87 100644 --- a/docs/class-ApiAxle.Shared.Config.html +++ b/docs/class-ApiAxle.Shared.Config.html @@ -125,8 +125,8 @@

Class Config

Author: Phillip Shipley <phillip@phillipshipley.com>
- Located at src/ApiAxle/Shared/Config.php
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
+ Located at src/ApiAxle/Shared/Config.php
@@ -144,7 +144,7 @@

Class Config

# - __construct( array $config = array() ) + __construct( array $config = array() )
@@ -184,7 +184,7 @@

Parameters

# - setConfg( array $config ) + setConfg( array $config )
@@ -222,7 +222,7 @@

Parameters

# - getConfig( ) + getConfig( )
@@ -259,7 +259,7 @@

Returns

# - loadConfigFile( string $file = false ) + loadConfigFile( string $file = false )
@@ -307,7 +307,7 @@

Throws

# - getEndpoint( ) + getEndpoint( )
@@ -336,7 +336,7 @@

Throws

# - setEndpoint( mixed $endpoint ) + setEndpoint( mixed $endpoint )
@@ -365,7 +365,7 @@

Throws

# - getKey( ) + getKey( )
@@ -394,7 +394,7 @@

Throws

# - setKey( mixed $key ) + setKey( mixed $key )
@@ -423,7 +423,7 @@

Throws

# - getSecret( ) + getSecret( )
@@ -452,7 +452,355 @@

Throws

# - setSecret( mixed $secret ) + setSecret( mixed $secret ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getSslVerifypeer( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setSslVerifypeer( mixed $ssl_verifypeer ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getSslCainfo( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setSslCainfo( mixed $ssl_cainfo ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getSslCapath( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setSslCapath( mixed $ssl_capath ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getProxyEnable( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setProxyEnable( mixed $proxy_enable ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getProxyHost( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setProxyHost( mixed $proxy_host ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + getProxyPort( ) + +
+ +
+ + +
+ + + + + public + + + + + +
+ # + setProxyPort( mixed $proxy_port )
@@ -481,7 +829,7 @@

Throws

# - getSignature( ) + getSignature( )
@@ -608,6 +956,184 @@

Returns

ApiAxle API shared secret for signing requests

+
+
+ + + + protected + boolean + + + + $ssl_verifypeer + + true +
+ # + +
+ +

Enable/Disable verification of peer certificate when calling API

+ +
+ + +
+ + + + protected + string + + + + $ssl_cainfo + + null +
+ # + +
+ +

Additional config for cURL providing alternative CA certificate info. The +name of a file holding one or more certificates to verify the peer with. +Requires absolute path. Use this option alongside CURLOPT_SSL_VERIFYPEER.

+ +
+ + +
+ + + + protected + string + + + + $ssl_capath + + null +
+ # + +
+ +

Additional config for cURL providing alternative CA certificate path. A +directory that holds multiple CA certificates. Use this option alongside +CURLOPT_SSL_VERIFYPEER.

+ +
+ + +
+ + + + protected + boolean + + + + $proxy_enable + + false +
+ # + +
+ +

Enable/disable usage of a proxy server

+ +
+ + +
+ + + + protected + string + + + + $proxy_host + + +
+ # + +
+ +

Set proxy server hostname

+ +
+ + +
+ + + + protected + string + + + + $proxy_port + + +
+ # + +
+ +

Set proxy server port

+ +
+ +
diff --git a/docs/class-ApiAxle.Shared.HttpRequest.html b/docs/class-ApiAxle.Shared.HttpRequest.html index bd627e3..2aa17e5 100644 --- a/docs/class-ApiAxle.Shared.HttpRequest.html +++ b/docs/class-ApiAxle.Shared.HttpRequest.html @@ -130,8 +130,8 @@

Class HttpRequest

Author: Phillip Shipley <phillip@phillipshipley.com>
- Located at src/ApiAxle/Shared/HttpRequest.php
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
+ Located at src/ApiAxle/Shared/HttpRequest.php
@@ -149,7 +149,7 @@

Class HttpRequest

# - request( string $uri, string $method = 'GET', string|array $postfields = false, mixed $headers = false ) + request( string $uri, string $method = 'GET', string|array $postfields = false, mixed $headers = false, mixed $config = false )
@@ -176,6 +176,8 @@

Parameters

string|array
$postfields If using POST, this parameter will be set as CURLOPT_POSTFIELDS
$headers
+
$config
+

Returns

diff --git a/docs/class-ApiAxle.Shared.ItemList.html b/docs/class-ApiAxle.Shared.ItemList.html index 674d867..672e623 100644 --- a/docs/class-ApiAxle.Shared.ItemList.html +++ b/docs/class-ApiAxle.Shared.ItemList.html @@ -137,7 +137,7 @@

Class ItemList

Author: Phillip Shipley <phillip@phillipshipley.com>
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
Located at src/ApiAxle/Shared/ItemList.php
diff --git a/docs/class-ApiAxle.Shared.Utilities.html b/docs/class-ApiAxle.Shared.Utilities.html index 3534a6d..c63a8d9 100644 --- a/docs/class-ApiAxle.Shared.Utilities.html +++ b/docs/class-ApiAxle.Shared.Utilities.html @@ -124,7 +124,7 @@

Class Utilities

Author: Phillip Shipley <phillip@phillipshipley.com>
+href="mailto:phillip@phillipshipley.com">phillip@phillipshipley.com>
Located at src/ApiAxle/Shared/Utilities.php
diff --git a/docs/source-class-ApiAxle.Api.Api.html b/docs/source-class-ApiAxle.Api.Api.html index f4b88cb..cc40dd2 100644 --- a/docs/source-class-ApiAxle.Api.Api.html +++ b/docs/source-class-ApiAxle.Api.Api.html @@ -100,7 +100,7 @@

Classes

3: * ApiAxle (https://github.com/fillup/apiaxle-module/) 4: * 5: * @link https://github.com/fillup/apiaxle-module for the canonical source repository - 6: * @license GPLv2+ + 6: * @license MIT 7: */ 8: namespace ApiAxle\Api; 9: diff --git a/docs/source-class-ApiAxle.Api.Key.html b/docs/source-class-ApiAxle.Api.Key.html index 12e6a06..d3f8153 100644 --- a/docs/source-class-ApiAxle.Api.Key.html +++ b/docs/source-class-ApiAxle.Api.Key.html @@ -100,7 +100,7 @@

Classes

3: * ApiAxle (https://github.com/fillup/apiaxle-module/) 4: * 5: * @link https://github.com/fillup/apiaxle-module for the canonical source repository - 6: * @license GPLv2+ + 6: * @license MIT 7: */ 8: namespace ApiAxle\Api; 9: diff --git a/docs/source-class-ApiAxle.Api.Keyring.html b/docs/source-class-ApiAxle.Api.Keyring.html index 8ac93bb..d553285 100644 --- a/docs/source-class-ApiAxle.Api.Keyring.html +++ b/docs/source-class-ApiAxle.Api.Keyring.html @@ -100,7 +100,7 @@

Classes

3: * ApiAxle (https://github.com/fillup/apiaxle-module/) 4: * 5: * @link https://github.com/fillup/apiaxle-module for the canonical source repository - 6: * @license GPLv2+ + 6: * @license MIT 7: */ 8: namespace ApiAxle\Api; 9: diff --git a/docs/source-class-ApiAxle.Shared.ApiException.html b/docs/source-class-ApiAxle.Shared.ApiException.html index 38a4a39..8a75236 100644 --- a/docs/source-class-ApiAxle.Shared.ApiException.html +++ b/docs/source-class-ApiAxle.Shared.ApiException.html @@ -105,7 +105,7 @@

Exceptions

3: * ApiAxle (https://github.com/fillup/apiaxle-module) 4: * 5: * @link https://github.com/fillup/apiaxle-module for the canonical source repository - 6: * @license GPLv2+ + 6: * @license MIT 7: */ 8: namespace ApiAxle\Shared; 9: diff --git a/docs/source-class-ApiAxle.Shared.Config.html b/docs/source-class-ApiAxle.Shared.Config.html index d46d19b..3c35ac7 100644 --- a/docs/source-class-ApiAxle.Shared.Config.html +++ b/docs/source-class-ApiAxle.Shared.Config.html @@ -105,7 +105,7 @@

Exceptions

3: * ApiAxle (https://github.com/fillup/apiaxle-module/) 4: * 5: * @link https://github.com/fillup/apiaxle-module for the canonical source repository - 6: * @license GPLv2+ + 6: * @license MIT 7: */ 8: namespace ApiAxle\Shared; 9: @@ -139,145 +139,266 @@

Exceptions

37: protected $secret; 38: 39: /** - 40: * Initialization status - 41: */ - 42: private $isInitialized = false; - 43: - 44: /** - 45: * Construct config object - 46: * - 47: * If config is provided, parse it and setup object - 48: * - 49: * @param array $config - 50: */ - 51: public function __construct($config = array()) - 52: { - 53: if($config && is_array($config) && count($config) > 0){ - 54: $this->setConfg($config); - 55: } elseif($config instanceof \ApiAxle\Shared\Config){ - 56: $this->setEndpoint($config->getEndpoint()); - 57: $this->setKey($config->getKey()); - 58: $this->setSecret($config->getSecret()); - 59: } else { - 60: $this->loadConfigFile(); - 61: } - 62: - 63: if(!is_null($this->endpoint) && !is_null($this->key) && !is_null($this->secret)){ - 64: $this->isInitialized = true; - 65: } - 66: } - 67: - 68: /** - 69: * Setup configuration - 70: * - 71: * @param array $config - 72: */ - 73: public function setConfg($config) - 74: { - 75: $this->setEndpoint(isset($config['endpoint']) ? $config['endpoint'] : false); - 76: $this->setKey(isset($config['key']) ? $config['key'] : false); - 77: $this->setSecret(isset($config['secret']) ? $config['secret'] : false); - 78: } - 79: - 80: /** - 81: * Return configuration settings as array - 82: * - 83: * @return array - 84: */ - 85: public function getConfig() - 86: { - 87: return array( - 88: 'endpoint' => $this->endpoint, - 89: 'key' => $this->key, - 90: 'secret' => $this->secret - 91: ); - 92: } - 93: - 94: /** - 95: * Load configuration from default config file unless alternate file is specified - 96: * - 97: * @param string $file - 98: * @return boolean - 99: * @throws Exception -100: */ -101: public function loadConfigFile($file=false) -102: { -103: $file = $file ?: __DIR__.'/../../../config/config.local.php'; -104: if(file_exists($file)){ -105: $config = include $file; -106: if(is_array($config) && count($config) > 0){ -107: $this->setConfg($config); -108: return true; -109: } -110: } -111: throw new \Exception('Unable to load configuration from file '.$file, '100'); -112: } -113: -114: public function getEndpoint() -115: { -116: return $this->endpoint; -117: } -118: -119: public function setEndpoint($endpoint) -120: { -121: $url = filter_var($endpoint, FILTER_VALIDATE_URL); -122: if($url){ -123: if(preg_match('/^http[s]{0,1}:\/\//', $endpoint)){ -124: $this->endpoint = $endpoint; -125: } else { -126: throw new \Exception('Endpoint must start with http:// or https://',101); -127: } -128: } else { -129: throw new \Exception('Invalid URL specified for Endpoint.',102); -130: } -131: -132: return $this; -133: } -134: -135: public function getKey() -136: { -137: return $this->key; -138: } -139: -140: public function setKey($key) -141: { -142: $this->key = $key; -143: } -144: -145: public function getSecret() -146: { -147: return $this->secret; -148: } -149: -150: public function setSecret($secret) -151: { -152: $this->secret = $secret; + 40: * Enable/Disable verification of peer certificate when calling API + 41: * + 42: * @var bool $ssl_verifypeer + 43: */ + 44: protected $ssl_verifypeer = true; + 45: + 46: /** + 47: * Additional config for cURL providing alternative CA certificate info. + 48: * The name of a file holding one or more certificates to verify the peer with. + 49: * Requires absolute path. + 50: * Use this option alongside CURLOPT_SSL_VERIFYPEER. + 51: * + 52: * @link http://us2.php.net/manual/en/function.curl-setopt.php cURL configuration options + 53: * @var string $ssl_cainfo + 54: */ + 55: protected $ssl_cainfo = null; + 56: + 57: /** + 58: * Additional config for cURL providing alternative CA certificate path. + 59: * A directory that holds multiple CA certificates. + 60: * Use this option alongside CURLOPT_SSL_VERIFYPEER. + 61: * + 62: * @link http://us2.php.net/manual/en/function.curl-setopt.php cURL configuration options + 63: * @var string $ssl_capath + 64: */ + 65: protected $ssl_capath = null; + 66: + 67: /** + 68: * Enable/disable usage of a proxy server + 69: * + 70: * @var bool $proxy_enable + 71: */ + 72: protected $proxy_enable = false; + 73: + 74: /** + 75: * Set proxy server hostname + 76: * + 77: * @var string $proxy_host + 78: */ + 79: protected $proxy_host; + 80: + 81: /** + 82: * Set proxy server port + 83: * + 84: * @var string $proxy_port + 85: */ + 86: protected $proxy_port; + 87: + 88: /** + 89: * Initialization status + 90: */ + 91: private $isInitialized = false; + 92: + 93: /** + 94: * Construct config object + 95: * + 96: * If config is provided, parse it and setup object + 97: * + 98: * @param array $config + 99: */ +100: public function __construct($config = array()) +101: { +102: if($config && is_array($config) && count($config) > 0){ +103: $this->setConfg($config); +104: } elseif($config instanceof \ApiAxle\Shared\Config){ +105: $this->setEndpoint($config->getEndpoint()); +106: $this->setKey($config->getKey()); +107: $this->setSecret($config->getSecret()); +108: $this->setSslVerifypeer($config->getSslVerifypeer()); +109: $this->setSslCainfo($config->getSslCainfo()); +110: $this->setSslCapath($config->getSslCapath()); +111: $this->setProxyEnable($config->getProxyEnable()); +112: $this->setProxyHost($config->getProxyHost()); +113: $this->setProxyPort($config->getProxyPort()); +114: } else { +115: $this->loadConfigFile(); +116: } +117: +118: if(!is_null($this->endpoint) && !is_null($this->key) && !is_null($this->secret)){ +119: $this->isInitialized = true; +120: } +121: } +122: +123: /** +124: * Setup configuration +125: * +126: * @param array $config +127: */ +128: public function setConfg($config) +129: { +130: $this->setEndpoint(isset($config['endpoint']) ? $config['endpoint'] : false); +131: $this->setKey(isset($config['key']) ? $config['key'] : false); +132: $this->setSecret(isset($config['secret']) ? $config['secret'] : false); +133: $this->setSslVerifypeer(isset($config['ssl_verifypeer']) ? $config['ssl_verifypeer'] : true); +134: $this->setSslCainfo(isset($config['ssl_cainfo']) ? $config['ssl_cainfo'] : true); +135: $this->setSslCapath(isset($config['ssl_capath']) ? $config['ssl_capath'] : true); +136: $this->setProxyEnable(isset($config['proxy_enable']) ? $config['proxy_enable'] : false); +137: $this->setProxyHost(isset($config['proxy_host']) ? $config['proxy_host'] : null); +138: $this->setProxyPort(isset($config['proxy_port']) ? $config['proxy_port'] : null); +139: } +140: +141: /** +142: * Return configuration settings as array +143: * +144: * @return array +145: */ +146: public function getConfig() +147: { +148: return array( +149: 'endpoint' => $this->endpoint, +150: 'key' => $this->key, +151: 'secret' => $this->secret +152: ); 153: } 154: 155: /** -156: * Generate an API signature based on key and shared secret. +156: * Load configuration from default config file unless alternate file is specified 157: * -158: * Uses hmac_sha1 to generate hash. There is a six second window where the -159: * hash is valid and ApiAxle will check the hash for three seconds before -160: * and after the call to validate it. Be sure you are using a network time -161: * server to keep your servers in sync with correct time -162: * -163: * Returns false if a sharedSecret is not set for the key -164: * -165: * @return boolean -166: * @return string -167: */ -168: public function getSignature() -169: { -170: if(!is_null($this->secret)){ -171: $api_sig = hash_hmac('sha1', time().$this->getKey(), $this->getSecret()); -172: return $api_sig; -173: } else { -174: return false; -175: } -176: } -177: -178: } +158: * @param string $file +159: * @return boolean +160: * @throws Exception +161: */ +162: public function loadConfigFile($file=false) +163: { +164: $file = $file ?: __DIR__.'/../../../config/config.local.php'; +165: if(file_exists($file)){ +166: $config = include $file; +167: if(is_array($config) && count($config) > 0){ +168: $this->setConfg($config); +169: return true; +170: } +171: } +172: throw new \Exception('Unable to load configuration from file '.$file, '100'); +173: } +174: +175: public function getEndpoint() +176: { +177: return $this->endpoint; +178: } +179: +180: public function setEndpoint($endpoint) +181: { +182: $url = filter_var($endpoint, FILTER_VALIDATE_URL); +183: if($url){ +184: if(preg_match('/^http[s]{0,1}:\/\//', $endpoint)){ +185: $this->endpoint = $endpoint; +186: } else { +187: throw new \Exception('Endpoint must start with http:// or https://',101); +188: } +189: } else { +190: throw new \Exception('Invalid URL specified for Endpoint.',102); +191: } +192: +193: return $this; +194: } +195: +196: public function getKey() +197: { +198: return $this->key; +199: } +200: +201: public function setKey($key) +202: { +203: $this->key = $key; +204: } +205: +206: public function getSecret() +207: { +208: return $this->secret; +209: } +210: +211: public function setSecret($secret) +212: { +213: $this->secret = $secret; +214: } +215: +216: public function getSslVerifypeer() +217: { +218: return $this->ssl_verifypeer; +219: } +220: +221: public function setSslVerifypeer($ssl_verifypeer) +222: { +223: $this->ssl_verifypeer = $ssl_verifypeer; +224: } +225: +226: public function getSslCainfo() +227: { +228: return $this->ssl_cainfo; +229: } +230: +231: public function setSslCainfo($ssl_cainfo) +232: { +233: $this->ssl_cainfo = $ssl_cainfo; +234: } +235: +236: public function getSslCapath() +237: { +238: return $this->ssl_capath; +239: } +240: +241: public function setSslCapath($ssl_capath) +242: { +243: $this->ssl_capath = $ssl_capath; +244: } +245: +246: public function getProxyEnable() +247: { +248: return $this->proxy_enable; +249: } +250: +251: public function setProxyEnable($proxy_enable) +252: { +253: $this->proxy_enable = $proxy_enable; +254: } +255: +256: public function getProxyHost() +257: { +258: return $this->proxy_host; +259: } +260: +261: public function setProxyHost($proxy_host) +262: { +263: $this->proxy_host = $proxy_host; +264: } +265: +266: public function getProxyPort() +267: { +268: return $this->proxy_port; +269: } +270: +271: public function setProxyPort($proxy_port) +272: { +273: $this->proxy_port = $proxy_port; +274: } +275: +276: /** +277: * Generate an API signature based on key and shared secret. +278: * +279: * Uses hmac_sha1 to generate hash. There is a six second window where the +280: * hash is valid and ApiAxle will check the hash for three seconds before +281: * and after the call to validate it. Be sure you are using a network time +282: * server to keep your servers in sync with correct time +283: * +284: * Returns false if a sharedSecret is not set for the key +285: * +286: * @return boolean +287: * @return string +288: */ +289: public function getSignature() +290: { +291: if(!is_null($this->secret)){ +292: $api_sig = hash_hmac('sha1', time().$this->getKey(), $this->getSecret()); +293: return $api_sig; +294: } else { +295: return false; +296: } +297: } +298: +299: } -
 1: <?php
- 2: /**
- 3:  * ApiAxle (https://github.com/fillup/apiaxle-module/)
- 4:  *
- 5:  * @link      https://github.com/fillup/apiaxle-module for the canonical source repository
- 6:  * @license   GPLv2+
- 7:  */
- 8: 
- 9: namespace ApiAxle\Shared;
-10: 
-11: /**
-12:  * HttpRequest class provides a very simple wrapper for using cURL functions.
-13:  * 
-14:  * Provides a single static function to make an HTTP request and get the results.
-15:  * If there is an error returned from the server it will throw an \ErrorException
-16:  * with the error message and error number returned from curl_exec.
-17:  * 
-18:  * @author Phillip Shipley <phillip@phillipshipley.com>
-19:  * 
-20:  */
-21: class HttpRequest
-22: {   
-23:     /**
-24:      * Make an HTTP Request
-25:      * 
-26:      * Simplified interface to cURL methods.
-27:      * 
-28:      * @link http://php.net/curl
-29:      * @param string $uri The URI to make the request to. For GET requests it
-30:      *   should include all parameters. This is passed as the CURLOPT_URL
-31:      * @param string $method Set to either GET (default) or POST
-32:      * @param string|array $postfields If using POST, this parameter will be 
-33:      *   set as CURLOPT_POSTFIELDS
-34:      * @return string On successful HTTP request, it will return the body of the
-35:      *   response as a string.
-36:      * @throws \ErrorException On error making the HTTP request.
-37:      */
-38:     public static function request($uri,$method='GET',$postfields=false,$headers=false) {
-39:         $ch = curl_init();
-40:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-41:         curl_setopt($ch, CURLOPT_URL, $uri);
-42:         curl_setopt($ch, CURLINFO_HEADER_OUT, true);
-43:         /**
-44:          * Added for debugging with Charles proxy
-45:          */
-46: //        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1');
-47: //        curl_setopt($ch, CURLOPT_PROXYPORT, '8888');
-48: //        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
-49: //        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-50:         
-51:         
-52:         $method = strtoupper($method);
-53:         if($method == 'GET'){
-54:             curl_setopt($ch, CURLOPT_HTTPGET, true);
-55:         } elseif($method == 'POST') {
-56:             curl_setopt($ch, CURLOPT_POST, true);
-57:         } elseif($method == 'PUT'){
-58:             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
-59:         } elseif($method == 'DELETE'){
-60:             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-61:         }
-62:         
-63:         if($postfields && ($method == 'POST' || $method == 'PUT')){
-64:             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
-65:         }
-66:         if($headers && is_array($headers)){
-67:             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-68:         }
-69:         
-70:         $response = curl_exec($ch);
-71:         $info = curl_getinfo($ch);
-72:         if($response){
-73:             return $response;
-74:         } else {
-75:             if($info['http_code'] == 204){
-76:                 $result = array(
-77:                     'success' => true
-78:                 );
-79:                 return json_encode($result);
-80:             } else {
-81:                 $curl_errno = curl_errno($ch);
-82:                 if($curl_errno == 0){
-83:                     $code = $info['http_code'];
-84:                 } else {
-85:                     $code = $curl_errno;
-86:                 }
-87:                 throw new \ErrorException('Http Request Failed: '.$uri.' (HTTP Status: '.$info['http_code'].') with error: '.  
-88:                     curl_error($ch), 210);
-89:             }
-90:         }
-91:     }
-92: }
+
  1: <?php
+  2: /**
+  3:  * ApiAxle (https://github.com/fillup/apiaxle-module/)
+  4:  *
+  5:  * @link      https://github.com/fillup/apiaxle-module for the canonical source repository
+  6:  * @license   MIT
+  7:  */
+  8: 
+  9: namespace ApiAxle\Shared;
+ 10: 
+ 11: /**
+ 12:  * HttpRequest class provides a very simple wrapper for using cURL functions.
+ 13:  * 
+ 14:  * Provides a single static function to make an HTTP request and get the results.
+ 15:  * If there is an error returned from the server it will throw an \ErrorException
+ 16:  * with the error message and error number returned from curl_exec.
+ 17:  * 
+ 18:  * @author Phillip Shipley <phillip@phillipshipley.com>
+ 19:  * 
+ 20:  */
+ 21: class HttpRequest
+ 22: {   
+ 23:     /**
+ 24:      * Make an HTTP Request
+ 25:      * 
+ 26:      * Simplified interface to cURL methods.
+ 27:      * 
+ 28:      * @link http://php.net/curl
+ 29:      * @param string $uri The URI to make the request to. For GET requests it
+ 30:      *   should include all parameters. This is passed as the CURLOPT_URL
+ 31:      * @param string $method Set to either GET (default) or POST
+ 32:      * @param string|array $postfields If using POST, this parameter will be 
+ 33:      *   set as CURLOPT_POSTFIELDS
+ 34:      * @return string On successful HTTP request, it will return the body of the
+ 35:      *   response as a string.
+ 36:      * @throws \ErrorException On error making the HTTP request.
+ 37:      */
+ 38:     public static function request($uri,$method='GET',$postfields=false,$headers=false,$config=false) {
+ 39:         $ch = curl_init();
+ 40:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ 41:         curl_setopt($ch, CURLOPT_URL, $uri);
+ 42:         curl_setopt($ch, CURLINFO_HEADER_OUT, true);
+ 43:         if($config){
+ 44:             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $config->getSslVerifypeer());
+ 45:             if($config->getSslVerifypeer()){
+ 46:                 if(!is_null($config->getSslCainfo())){
+ 47:                     curl_setopt($ch, CURLOPT_CAINFO, $config->getSslCainfo());
+ 48:                 }
+ 49:                 if(!is_null($config->getSslCapath())){
+ 50:                     curl_setopt($ch, CURLOPT_CAPATH, $config->getSslCapath());
+ 51:                 }
+ 52:             }
+ 53:             
+ 54:             if($config->getProxyEnable()){
+ 55:                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $config->getProxyEnable());
+ 56:                 curl_setopt($ch, CURLOPT_PROXY, $config->getProxyHost());
+ 57:                 curl_setopt($ch, CURLOPT_PROXYPORT, $config->getProxyPort());
+ 58:             }
+ 59:         }
+ 60:         
+ 61:         $method = strtoupper($method);
+ 62:         if($method == 'GET'){
+ 63:             curl_setopt($ch, CURLOPT_HTTPGET, true);
+ 64:         } elseif($method == 'POST') {
+ 65:             curl_setopt($ch, CURLOPT_POST, true);
+ 66:         } elseif($method == 'PUT'){
+ 67:             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
+ 68:         } elseif($method == 'DELETE'){
+ 69:             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
+ 70:         }
+ 71:         
+ 72:         if($postfields && ($method == 'POST' || $method == 'PUT')){
+ 73:             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
+ 74:         }
+ 75:         if($headers && is_array($headers)){
+ 76:             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+ 77:         }
+ 78:         
+ 79:         $response = curl_exec($ch);
+ 80:         $info = curl_getinfo($ch);
+ 81:         if($response){
+ 82:             return $response;
+ 83:         } else {
+ 84:             if($info['http_code'] == 204){
+ 85:                 $result = array(
+ 86:                     'success' => true
+ 87:                 );
+ 88:                 return json_encode($result);
+ 89:             } else {
+ 90:                 $curl_errno = curl_errno($ch);
+ 91:                 if($curl_errno == 0){
+ 92:                     $code = $info['http_code'];
+ 93:                 } else {
+ 94:                     $code = $curl_errno;
+ 95:                 }
+ 96:                 throw new \ErrorException('Http Request Failed: '.$uri.' (HTTP Status: '.$info['http_code'].') with error: '.  
+ 97:                     curl_error($ch), 210);
+ 98:             }
+ 99:         }
+100:     }
+101: }