I have been developing this class, and was wondering if anyone had any thoughts on how I can improve the performance of it.
<?php
class Something {
private $APIUsername, $APIPassword;
private $APIurl = 'somesite.com';
function __construct ($APIUsername = '', $APIPassword = '') {
try {
if (!$APIUsername || !$APIPassword) {
throw new Exception('You must specify a valid API username and password.');
}
$this->APIUsername = $APIUsername;
$this->APIPassword = $APIPassword;
} catch (Exception $e) {
die($e->getMessage());
}
}
/*
* Authenticate Account
*/
public function authenticate ($EmailAddress, $Password) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data = $this->setJSON($data);
$result = $this->fetch('authenticate', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Get Contacts
*/
public function contacts ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;
$data = $this->setJSON($data);
$result = $this->fetch('contacts', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Get Transactions
*/
public function transactions ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;
$data = $this->setJSON($data);
$result = $this->fetch('transactions', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Validate PIN
*/
public function validatepin ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;
$data = $this->setJSON($data);
$result = $this->fetch('validatepin', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Get Account Balance
*/
public function balance ($EmailAddress, $Password) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data = $this->setJSON($data);
$result = $this->fetch('balance', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Get Account Information
*/
public function account_information ($EmailAddress, $Password) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data = $this->setJSON($data);
$result = $this->fetch('account_information', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Send Money
*/
public function send ($EmailAddress, $Password, $PIN, $DestinationID, $Amount, $Notes = '', $FundsSource = '') {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;
$data['DestinationID'] = $DestinationID;
$data['Amount'] = $Amount;
$data['Notes'] = urlencode($Notes);
$data['FundsSource'] = $FundsSource;
$data = $this->setJSON($data);
$result = $this->fetch('send', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Send (Sender Assumes Transaction Costs)
*/
public function send_assume_costs ($EmailAddress, $Password, $PIN, $DestinationID, $Amount, $Description = '') {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;
$data['DestinationID'] = $DestinationID;
$data['Amount'] = $Amount;
$data['Description'] = urlencode($Description);
$data = $this->setJSON($data);
$result = $this->fetch('send_assume_costs', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}
return $result;
}
/*
* Set JSON Data
*/
private function setJSON ($data) {
$a = array();
$a['APIUsername'] = $this->APIUsername;
$a['APIPassword'] = $this->APIPassword;
foreach ($data as $key => $value) {
$a[$key] = $value;
}
return json_encode($a);
}
/*
* Helper method that talks to teh API
*/
private function fetch($APIMethod, $data) {
$c = curl_init($this->APIurl.$APIMethod);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$returned = curl_exec($c);
if ($returned === false) {
throw new Exception(curl_error($c)); return;
}
curl_close($c);
if (json_decode($returned)) {
return $returned;
} else {
throw new Exception('Invalid Service Request.'); return;
}
}
}
?>
setJSON
andfetch
methods used outside your class at all? \$\endgroup\$