API integration - PHP example

Posted by Jeff Rasmussen. Last updated: Fri, Oct 11, 2013

In Ubivox you have access to a powerful API, which makes it possible to integrate web applications to Ubivox.

The following is an example of how to do it in PHP, using Zends Framework, which can be downloaded at zend.com. Download Zend Framework Minimal og unzip it to a separat folder. Set include-path* to point to the library folder, and then run the Zend magic:

set_include_path('/sti/til/ZendFramework-1.10.8-minimal/library/');

require_once('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();

Adjust the following defines.

define('USERNAME', 'username-til-API');
define('PASSWORD', 'password-til-API');
define('LIST_ID', 42);

You can access the information from your account, go to Account -> Users and access

Then click the View developer access (FTP & API) button

Then you have access to the necessary credentials.

The following code instantiates the $client-object, which is used to communicate with the service.

define('HOSTNAME', 'https://'.USERNAME.'.clients.ubivox.com/xmlrpc/');

$http = new Zend_Http_Client(HOSTNAME, array('keepalive'=>true));
$http->setAuth(USERNAME, PASSWORD, Zend_Http_Client::AUTH_BASIC);

$client = new Zend_XmlRpc_Client(HOSTNAME, $http);

Now you should have connection to the API. Then you can do the following, which collects information about the recipient example: foo@eksempel.dk:

try {
  $response = $client->call('ubivox.get_subscriber',
                            array('foo@eksempel.dk'));

  var_dump($response);
} catch (Zend_XmlRpc_Client_FaultException $e) {
  var_dump($e);
}

If you, for example, would like to create a subscription, create_subscription, do the following:

try {

  $client->call('ubivox.create_subscription',
                array('foo@eksempel.dk', LIST_ID, true));

} catch(Zend_XmlRpc_Client_HttpException $e) {

  // Connection error
  var_dump($e);

} catch(Zend_XmlRpc_Client_FaultException $e) {

  switch($e->getCode()) {
    case 1003: // Allready subscribed on this list in ubivox
      break;
    case 2001: // Invalid mailing list
      break;
    case 1001: // E-mail address is invalid
      break;
  }

} catch(Exception $e) {

  // Unknown error
  var_dump($e);

}

// The subscription was created

To see the full API documentation, go to the top menu -> Account -> API (XML-RPC). The documentation for Zends XMLRPX-framework, is found on zend.com search for xmlrpc.


What next?

Read more articles on these subjects: API, Integration

Keywords

See other articles with these keywords:


Languages

See this article in:


About the author

Jeff Rasmussen
Communications Officer

Adept at strategic communication, especially external. Handles the Ubivox LinkedIn profile and marketing campaigns.


Further questions

Do not hesitate to contact us if you have any questions.

Start livechat
Write e-mail


Get in touch if you want to try Ubivox -
send us an e-mail at support@ubivox.com.