Adding optional SSL support

This commit is contained in:
Jaisen Mathai 2013-12-17 23:21:32 -08:00
parent eaa6be4f81
commit 2a44dea3ed
2 changed files with 15 additions and 5 deletions

View file

@ -116,18 +116,23 @@ class OpenPhotoOAuth
return $resp; return $resp;
} }
public function useSSL($bool)
{
$this->protocol = $bool ? 'https' : 'http';
}
private function constructEndpoint($endpoint, $includeConsumerKey = false) private function constructEndpoint($endpoint, $includeConsumerKey = false)
{ {
if($includeConsumerKey) if($includeConsumerKey)
{ {
if(stristr($endpoint, '?') === false) if(stristr($endpoint, '?') === false)
return sprintf('http://%s%s?oauth_consumer_key=%s', $this->host, $endpoint, $this->consumerKey); return sprintf('%s://%s%s?oauth_consumer_key=%s', $this->protocol, $this->host, $endpoint, $this->consumerKey);
else else
return sprintf('http://%s%s&oauth_consumer_key=%s', $this->host, $endpoint, $this->consumerKey); return sprintf('%s://%s%s&oauth_consumer_key=%s', $this->protocol, $this->host, $endpoint, $this->consumerKey);
} }
else else
{ {
return sprintf('http://%s%s', $this->host, $endpoint); return sprintf('%s://%s%s', $this->protocol, $this->host, $endpoint);
} }
} }
} }

View file

@ -5,8 +5,10 @@ $consumerSecret = getenv('consumerSecret');
$token = getenv('token'); $token = getenv('token');
$tokenSecret = getenv('tokenSecret'); $tokenSecret = getenv('tokenSecret');
$arguments = getopt('F:X:h:e:pv', array('encode::')); $arguments = getopt('F:X:h:e:pvs', array('encode::'));
$host = 'localhost'; $host = 'localhost';
$useSSL = false;
if(isset($arguments['h'])) if(isset($arguments['h']))
$host = $arguments['h']; $host = $arguments['h'];
$method = 'get'; $method = 'get';
@ -21,6 +23,8 @@ if(isset($arguments['p']))
$verbose = false; $verbose = false;
if(isset($arguments['v'])) if(isset($arguments['v']))
$verbose = true; $verbose = true;
if(isset($arguments['s']))
$useSSL = true;
$encode = false; $encode = false;
if(isset($arguments['encode'])) if(isset($arguments['encode']))
$encode = true; $encode = true;
@ -40,13 +44,14 @@ if(isset($arguments['F']))
include 'OpenPhotoOAuth.php'; include 'OpenPhotoOAuth.php';
$client = new OpenPhotoOAuth($host, $consumerKey, $consumerSecret, $token, $tokenSecret); $client = new OpenPhotoOAuth($host, $consumerKey, $consumerSecret, $token, $tokenSecret);
$client->useSSL($useSSL);
if($method == 'get') if($method == 'get')
$resp = $client->get($endpoint, $fields); $resp = $client->get($endpoint, $fields);
elseif($method == 'post') elseif($method == 'post')
$resp = $client->post($endpoint, $fields); $resp = $client->post($endpoint, $fields);
if($verbose) if($verbose)
echo sprintf("==========\nMethod: %s\nHost: %s\nEndpoint: %s\n==========\n\n", $method, $host, $endpoint); echo sprintf("==========\nMethod: %s\nHost: %s\nEndpoint: %s\nSSL: %s\n==========\n\n", $method, $host, $endpoint, $useSSL ? 'Yes' : 'No');
if($pretty) if($pretty)
echo indent($resp); echo indent($resp);