Creating a Custom Connector

Creating your own connector is simple.  First step is to download the source for a current connector as a guide.  This blog post will go into this topic step by step though many times it is helpful to have the full source of an implemented connector to look at.

For the purpose of this post the code samples will be from the php connector.

1)  If you desire a certain format for your messages make sure you do it prior to sending the message

2)  To start building the request you will need

    • create a hash of the content using your secret key
    • headers for your access key
      • hash
      • content length
      • content type being application/json.

3)  Create the hash using your Secret Key (dots are for concatenation).  Note the false parameter on the sha1 function which will return a 40-character hexadecimal number.  Take note that the input format to create the hash is <message>|<secret key>

$validation_string = $message.”|”.UL_SECRETKEY;

$computed_hash = sha1( $validation_string , false );

4)  Json encode the message so it has the format {“Content”:”[your message]”}

$fields = array( ‘Content’ => $message );

$json_data = json_encode( $fields );

5)  Set the headers

curl_setopt( $ch, CURLOPT_HTTPHEADER,

array(‘Content-Type: application/json’,

‘Content-Length: ‘.strlen( $json_data ),

 ‘Authorization: ‘.UL_ACCESSKEY,

 ‘MD5: ‘.$computed_hash ) );

6)  Set the post data

curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data );

7)  Send the http request to your submission url

8)  Handle the response, a 201 means the http post was successful.

9) Feel free to reach out to support for help
Some things to consider

  • Send asynchronously where possible
  • If a 201 response is not received consider a retry
  • Log locally as well as to Unified Logging

For a an example of a more robust connector look at the .NET Trace Listener implementation which sends everything async and has a retry queue if a message fails for some reason.

Comments are closed.