I have been developing this class, and was wondering if anyone had any thoughts on how I can improve the performance of it.<?phpclass 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;        }    }}?>
JSON API for some bank account
php;object oriented;php5
Some things I noticed.It seems like a lot/all of your public functions are simply doing the same thing, setting the functions params into an array, calling setJSON which simply appends the APIusername and APIpassword and then you fetch and catch any errors. You could have one method that does this and only methods that differ from this logic would do something else.looks like you can just use PHP's compact() function for most of what you are doing.For setJSON(), instead of looping through $data and remaking the same array, why not just add $data['APIUsername'] = $this->APIUsername; and $data['APIPassword'] = $this->APIPassword; and then return the json_encode($data)For your fetch() method, you could get rid of the curl requirement and just use streams in PHP using a combination of stream_context_create() and file_get_contents().I think it also might be useful to glance over this articles about Exceptions .If you really are just passing along data with the username and password attached, you may just want to just use the magic __call method, that will check an array of acceptable API methods to call and then take the params, compact, add username and password, and then call your fetch method with this data and return. Example below.<?phpclass API {    protected $_api_methods = array(        'balance' => array('EmailAddress', 'Password'),    );    private $_api_username;        private $_api_password;    private $_api_url = 'someurl.com';    public function __construct($username, $password)    {        if (!$username || !$password) {            throw new APIException('You must provide API credentials');        }        $this->_api_username = $username;        $this->_api_password = $password;    }    public function __call($method_name, $arguments)    {        if (!in_array($method_name, array_keys($this->_api_methods)) {            throw new APIException($method_name is not a valid API method);        }        $data = $this->setJSON($method_name, $arguments);        return $this->fetch($method_name, $data);    }    protected function setJSON($method_name, $data)    {        if (empty($data)) {            throw new APIException('No data provided');        }        $data = array_combine($this->_api_methods[$method_name], $data);        $data['api_username'] = $this->_api_username;        $data['api_password'] = $this->_api_password;        return json_encode($data);    }    protected function fetch($method, $data)    {        $context = stream_context_create(array(            'http' => array(              'method'  => 'GET',              'timeout' => 5,            ),        ));        try {            $ret = file_get_contents($this->_api_url . $method, false, $context);         } catch (APIException $e) {                $ret = $e->getMessage();        }        return $ret;    }}class APIException extends Exception {}