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 @@
__construct( array $config = array() )
+ __construct( array $config = array() )
setConfg( array $config )
+ setConfg( array $config )
getConfig( )
+ getConfig( )
loadConfigFile( string $file = false )
+ loadConfigFile( string $file = false )
getEndpoint( )
+ getEndpoint( )
setEndpoint( mixed $endpoint )
+ setEndpoint( mixed $endpoint )
getKey( )
+ getKey( )
setKey( mixed $key )
+ setKey( mixed $key )
getSecret( )
+ getSecret( )
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+
+ public
+
+
+
+ setProxyPort( mixed $proxy_port )
getSignature( )
+ getSignature( )
ApiAxle API shared secret for signing requests
+
+ protected
+ boolean
+
true
Enable/Disable verification of peer certificate when calling API
+ +
+ protected
+ string
+
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
+
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
+
false
Enable/disable usage of a proxy server
+ +
+ protected
+ string
+
Set proxy server hostname
+ +
+ protected
+ string
+
Set proxy server port
+ +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 )
string|array
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: }