PHP Sample Code
This is a sample PHP script that:
- Authenticates.
- Uses web services to search for all top-level items that are currently self-registrable.
The script uses PEAR SOAP for its SOAP communication.
// append PEAR package path to include path set_include_path(get_include_path() . ';./' . 'lib'); require_once 'PEAR.php'; require_once 'Mail.php'; require_once 'SOAP/Client.php';
define('NAMESPACE', 'http://firmwater.com/webservices/lms/lmsservice1');
// utility function to create a LmsObject SOAP value object function getLmsObject($client, $name, $type, &$value) { if ($name == '') $name = 'LmsObject'; // create new soap value $lmsObject = new SOAP_Value('{' . NAMESPACE . '}' . $name, '{' . NAMESPACE . '}' . $type, $value, array('xsi:type' => $client->_getNamespacePrefix(NAMESPACE) . ':' . $type)); return $lmsObject; }
// get proxy $wsdl = new SOAP_WSDL('https://demo.firmwater.com/lms/webservices/LMSService1.asmx?wsdl'); $client = $wsdl->getProxy(); if (PEAR::isError($client)) { // error if unable to get proxy print('Unable to get proxy to connect to LMS: ' . $client->getMessage()); }
// set tracing $client->__options['trace'] = 1;
// login to web service $result = $client->Login('Firmwater', 'system', 'thepassword'); if (PEAR::isError($result)) { // error during login print('Error authenticating with LMS: ' . $result->getMessage()); }
// save soap header to be added to future calls $header = new SOAP_Header('{'. NAMESPACE . '}SessionHeader', NULL, array('sessionId' => $result->SessionId), 0);
// reset the end point $client->Url = $result->ServerUrl;
// add session id header $client->addHeader($header);
// get all top-level items that are self-registerable date_default_timezone_set('UTC'); $searchParams = array();
$param = array( 'FieldName' => 'IsActivity', 'Operation' => 'equals', 'SearchTerm' => 'true' ); $searchParams[] = getLmsObject($client, 'LmsSearchParam', 'LmsSearchParam', $param); $param = array( 'FieldName' => 'Activity.CanSelfRegister', 'Operation' => 'equals', 'SearchTerm' => 'true' ); $searchParams[] = getLmsObject($client, 'LmsSearchParam', 'LmsSearchParam', $param); $param = array( 'FieldName' => 'Activity.SelfRegisterStartDatetime', 'Operation' => 'leq', 'SearchTerm' => date('Y-m-d\TG:i:s') ); $searchParams[] = getLmsObject($client, 'LmsSearchParam', 'LmsSearchParam', $param); $param = array( 'FieldName' => 'Activity.SelfRegisterEndDatetime', 'Operation' => 'geq', 'SearchTerm' => date('Y-m-d\TG:i:s') ); $searchParams[] = getLmsObject($client, 'LmsSearchParam', 'LmsSearchParam', $param); // call web service $result = $client->Search('Firmwater', 'LmsItemObject', $searchParams, 'and'); if (PEAR::isError($result)) { // web service error print('Error searching: ' . $result->getMessage()); }
|