Loading...

Posted in: Forums > Getting Started

Subscribe to topic RSS

 

API with PHP Examples

Thu, 15 Dec '11 @ 11:58 PM
Hi

I am currently reviewing worketc for my business and I have certain ideas with what I want to be able to do to interact with it from my other systems. If these are possible through the API and easy enough to do with PHP then it will most likely be a goer for me. I have been trying to work out the API but there seems to be very few examples. I am a PHP developer but have had no experience with SOAP. Is there some examples that can be provided for simply connecting to the server and returning some data.

I have found the XML for the API at http://admin.worketc.com/xml and http://admin.worketc.com/xml?WSDL and a jscript example but nothing in PHP.

Any sort of direction here would be much appreciated.

Thank you

Luke
Jeff
Technical Director
Thu, 29 Dec '11 @ 12:11 PM
Luke,

I've put together a quick example as I find myself in the same boat as you when working with PHP/SOAP/WORKetc. This is not a final solution in the least but it does get you started on the right path. One thing I've found is when calling the request function, any arguments passed must be of a stdClass object and not associative array as is found in the authenticate method in the class. As I refine the class I will post updates here. Alternatively, you could possible look at using wsdl2php. However, this project has not been managed in quite some time and it looks as though there may be some issue implementing it against the API provided here at WORKetc. I've not tested it as I found myself wanting to save my time trying to fix a dead project and instead opted to create what I specifically needed for my own uses.

worketc.php

<?php
class WORKetc {

//Url of the webservice
private $baseUrl = null;
//Session key assigned by WORKetc
private $session_key;
//SOAP Client object
private $client;
//Full name of uesr accessing API
public $Fullname;

public function __construct($url)
{
//Check that url is properly formatted with http:// or https://
if (strripos($url, 'http://') === false)
{
//Add http:// if not found
$url = 'http://'.$url;
}

//Store url for later use
$this->baseUrl = $url;

//Create new SoapClient object
if ($url != null)
{
$this->client = new SoapClient($url);
}
}

public function authenticate($username, $password)
{
//Create array to hold credentials
$credentials = array("email" => $username, "pass" => $password);

//Try to authenticate with service
try
{
$result = $this->client->AuthenticateWebSafe($credentials);
}
catch (Exception $e)
{
//Attempt failed
return false;
}

//Success!
$this->session_key = $result->AuthenticateWebSafeResult->SessionKey;
$this->Fullname = $result->AuthenticateWebSafeResult->User->Name;
return true;
}

public function request($service, $args = null)
{
//Check for session_key
if ($this->session_key == null)
{
//It's going to fail so kill it now.
return false;
}

//Check if args is empty. Args must be an empty array if null
//as __soapcall requires two arguments regardless of information
if ($args == null)
{
$args = array();
}
//Call the webservice method
$result = $this->client->__soapCall($service, $args);
//Return result
return $result;
}
}
?>


index.php

<?php
require_once('worketc.php');

//Create new object
$client = new WORKetc("http://yourdomain.worketc.com/xml?wsdl");
//Send login information
$result = $client->authenticate("username", "password");

//Check that authentication worked
if ($result == true)
{
//Show the user that they are logged in
echo('Logged in: '.$client->Fullname);
//Get some information
$result = $client->request("GetSupportCaseTypes");
//Show information on the screen
echo("<pre>");
print_r($result);
echo("</pre>");
}
?>
Sat, 28 Jan '12 @ 8:47 PM
Thanks for this Jeff. I've been really busy so havent had much of a chance to play with this but I am now and so far so good. :)