Skip to content

Instantly share code, notes, and snippets.

@joebordes
Last active June 13, 2017 16:31
Show Gist options
  • Save joebordes/847c9ef7baa4ae035b04e4826177d270 to your computer and use it in GitHub Desktop.
Save joebordes/847c9ef7baa4ae035b04e4826177d270 to your computer and use it in GitHub Desktop.
coreBOS Webservice session expire test
  • Edit both files and set the $coreBOS_Basedir, $cbURL, $cbUserName and $cbAccessKey variables
    • $coreBOS_Basedir must point to the top of the directory where the coreBOS Webservice PHP library lives
    • the coreBOS Webservice library is not used, just the classes to inteface with http through cURL
  • Launch dologin.php
  • Edit doquery and set the $cbSessionID to the value returned from dologin.php
  • Launch doquery.php as many times as you want
<?php
$coreBOS_Basedir = 'vtwsclib';
$cbURL = 'http://localhost/coreboswork/webservice.php';
$cbUserName = 'admin';
$cbAccessKey = 'cdYTBpiMR9RfGgO';
require_once $coreBOS_Basedir . '/Net/HTTP_Client.php';
$httpc = new cbHTTP_Client($cbURL);
//getchallenge must be a GET request.
$response = $httpc->fetch_url("$cbURL?operation=getchallenge&username=$cbUserName");
echo "Raw response (json) GetChallenge";
var_dump($response);
//decode the json encode response from the server.
$jsonResponse = json_decode($response,true);
echo "Webservice response GetChallenge";
var_dump($jsonResponse);
//check for whether the requested operation was successful or not.
if($jsonResponse['success']==false) {
echo 'getchallenge failed:'.$jsonResponse['error']['message'];
echo "Challenge failed.";
} else {
//operation was successful get the token from the response.
$challengeToken = $jsonResponse['result']['token'];
echo "Challenge token: $challengeToken<br>";
}
//create md5 string concatenating user accesskey from my preference page
//and the challenge token obtained from get challenge result.
$generatedKey = md5($challengeToken.$cbAccessKey);
//login request must be POST request.
$response = $httpc->send_post_data($cbURL,
array('operation'=>'login', 'username'=>$cbUserName, 'accessKey'=>$generatedKey), true);
echo "Raw response (json) doLogin";
var_dump($response);
//decode the json encode response from the server.
$jsonResponse = json_decode($response,true);
echo "Webservice response doLogin";
var_dump($jsonResponse);
//operation was successful get the token from the response.
if($jsonResponse['success']==false) {
echo 'login failed:'.$jsonResponse['error']['message'];
echo "Error trying to log in!";
} else {
//login successful extract sessionId and userId from LoginResult so it can used for further calls.
$sessionId = $jsonResponse['result']['sessionName'];
$userId = $jsonResponse['result']['userId'];
echo "doLogin sessionId: $sessionId<br>";
echo "doLogin userId: $userId<br>";
}
?>
<?php
$coreBOS_Basedir = 'vtwsclib';
$cbURL = 'http://localhost/coreboswork/webservice.php';
require_once $coreBOS_Basedir . '/Net/HTTP_Client.php';
$httpc = new cbHTTP_Client($cbURL);
$cbSessionID = '4ce937d9593ffba9db0f2';
//query to select data from the server.
$query = "select firstname from Contacts limit 2;";
//urlencode as its sent over http.
$queryParam = urlencode($query);
//sessionId is obtained from login result.
$params = "sessionName=$cbSessionID&operation=query&query=$queryParam";
//query must be GET Request.
$response = $httpc->fetch_url("$cbURL?$params");
echo "Raw response (json) Query";
var_dump($response);
//decode the json encode response from the server.
$jsonResponse = json_decode($response, true);
echo "Webservice response Query";
var_dump($jsonResponse);
if($jsonResponse['success']==false) {
echo 'query failed: '.$jsonResponse['message'];
echo 'query failed!';
} else {
//Array of vtigerObjects
$retrievedObjects = $jsonResponse['result'];
if (count($retrievedObjects)>0) {
echo "<table class='table table-striped small table-condensed'><tr>";
foreach ($retrievedObjects[0] as $key=>$value) {
echo "<th>$key</th>";
}
echo "</tr>";
foreach ($retrievedObjects as $record) {
echo "<tr>";
foreach ($record as $value) {
echo "<td>&nbsp;$value</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "No records found!";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment