Pages

5/21/2012

Blackboard Connect 5 API with PHP

I've been working on connecting our Data Warehouse to the Blackboard Connect Service using the SOAP API's. For those interested, here is the quick and dirty way to do it on PHP 5.3

Create the SOAP client
$client = new SoapClient("https://Service.BlackboardConnect.com/Contact/v2/ContactService.asmx?wsdl",array ('trace' => 1));
Create Authorization Header
//Parameters
  $options = array(
       'DATE' => gmdate("Ymd"),
       'TIME' => gmdate("Hi"), 
       'API' => '*API KEY*',
       'SECRET => '*API SECRET*');

//Hash Parameters
$hash = md5(strtoupper($options['API']) . '|' . $options['SECRET'] . '|' . $options['DATE'] . '|' . $options['TIME']);

//Create Token
$authtoken = trim($hash . '|' . $options['API']);

$headerbody = array('Token' => $authtoken);

//Set SOAP Header
$header = new SOAPHeader('BBConnect.Service.Contact', 'AuthToken', $headerbody);
$client -> __setSoapHeaders($header);
I had to strip the header tag for the xml
$xml = simplexml_load_file(*path to file*);

$convert = $xml->asXML();
$xmldata = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $convert);
Create SOAP Variable
$input = new SoapVar( $xmldata, XSD_ANYXML );
 Submit Request
try {   
            $request = new StdClass();
            $request->inputXml = (object) array('any' => $input );
            $response = $client->UpdateContact($request);
        } catch (Exception $e) {
            echo "<h2>Exception Error!</h2>";
            echo $e -> getMessage();
            }

Get Response
echo "<h3>RESPONSE:</h3>";
echo htmlspecialchars($client->__getLastResponse());
Debug Request/Header
echo "<h3>REQUEST:</h3>";
echo htmlspecialchars($client->__getLastRequest());
echo "<h3>REQUEST HEADERS:</h3>";
echo htmlspecialchars($client->__getLastRequestHeaders());
Blackboard Connect 5 API Documentation

Pretty straight forward, if you have any questions let me know.

No comments:

Post a Comment