Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Last active March 14, 2017 15:32
Show Gist options
  • Select an option

  • Save ahebrank/0aa7929df89297c18cc968572abf0be9 to your computer and use it in GitHub Desktop.

Select an option

Save ahebrank/0aa7929df89297c18cc968572abf0be9 to your computer and use it in GitHub Desktop.
PHP mini API for connecting to Hobsons CRM
<?php
require_once('HobsonsConnect.php');
$clientname = '';
$passkey = '';
$remote = new HobsonsConnect($clientname, $passkey);
// list contact attributes
print_r($remote->getAllAttributes());
// list a contact
print_r($remote->getContacts(['firstname', 'lastname']));
// make a contact
$fields = array(
'firstname' => 'fTest',
'lastname' => 'lTest',
'email' => 'someone@example.com',
'phone' => '999-999-9999',
);
print_r($remote->createContact($fields, FALSE));
<?php
// some of this functionality is borrowed from another API found on Github that unfortunately contains private credentials
// hopefully this will have better SEO: Hobsons, HobsonsConnect, SOAP, WDSL
class HobsonsConnect {
var $client, $params;
function __construct($clientname, $passkey) {
$bridge_url = 'https://' . $clientname . '.askadmissions.net/ws/bridge.asmx?wsdl';
$this->client = new SoapClient($bridge_url, array('trace' => 1));
$this->params = array(
'ClientName' => $clientname,
'PassKey' => $passkey
);
}
function createAttributesXml($attributesArray){
if (empty($attributesArray)) {
return '';
}
$xmlString = '<attributes>';
foreach($attributesArray as $attribute){
$xmlString .= '<attribute><name>'.$attribute['name'].'</name>';
$xmlString .= isset($attribute['value']) ? '<value><![CDATA['.$attribute['value'].']]></value>' : '';
$xmlString .= isset($attribute['operator']) ? '<operator>'.$attribute['operator'].'</operator>': '';
$xmlString .= isset($attribute['order']) ? '<order>'.$attribute['order'].'</order>': '';
$xmlString .= '</attribute>';
}
$xmlString .= '</attributes>';
return $xmlString;
}
/**
* see if code == 1 in result
**/
function requestSuccess($result) {
return ($result->code == 1);
}
/**
* convert a key => val array into an array of xml attributes
**/
function attributize($fields) {
$atts = array();
foreach ($fields as $key => $val) {
if (!is_numeric($key)) {
$atts[] = array(
'name' => $key,
'value' => $val,
);
}
else {
$atts[] = array(
'name' => $val,
);
}
}
return $atts;
}
/**
* function to test API
**/
function getAllAttributes() {
$params = $this->params;
$response = $this->client->getAllAttributes($params);
return simplexml_load_string($response->GetAllAttributesResult, NULL, LIBXML_NOCDATA);
}
/**
* another test
**/
function getContacts($fields, $order = null, $criteria = null, $start = 1, $end = 1) {
$attributes = $this->attributize($fields);
$params = array_merge($this->params, array(
'StartingRecord' => $start,
'EndingRecord' => $end,
'SearchCriteriaXml'=> new SoapVar($this->createAttributesXML($criteria), XSD_STRING),
'AttributesXml'=> new SoapVar($this->createAttributesXML($attributes), XSD_STRING),
'OrderByXml'=> new SoapVar($this->createAttributesXML($order), XSD_STRING),
));
$response = $this->client->getContacts($params);
return simplexml_load_string($response->GetContactsResult, NULL, LIBXML_NOCDATA);
}
/**
* post a new contact
**/
function createContact($fields, $success_only = TRUE) {
$attributes = $this->attributize($fields);
$params = array_merge($this->params, array(
'AttributesXml'=> new SoapVar($this->createAttributesXML($attributes),XSD_STRING)
));
$response = $this->client->createContact($params);
$result = simplexml_load_string($response->CreateContactResult, NULL, LIBXML_NOCDATA);;
return $success_only? $this->requestSuccess($result) : $result;
}
/**
* update an existing contact
**/
function updateContact($id, $fields, $success_only = TRUE){
$attributes = $this->attributize($fields);
$params = array_merge($this->params, array(
'ContactID' => $id,
'AttributesXml' => new SoapVar($this->createAttributesXML($attributes), XSD_STRING)
));
$response = $this->client->updateContact($params);
$result = simplexml_load_string($response->UpdateContactResult, NULL, LIBXML_NOCDATA);;
return $success_only? $this->requestSuccess($result) : $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment