I've previously posted on the Smart Cloud for Social Business (SC4SB) demo application, which is written in Java and aimed respectively at a J2EE application server (e.g. Tomcat or WebSphere). But recently, I received a request to provide an example demonstrating the SC4SB API using PHP. Now I've not programmed in PHP in many years, but I was able to write a simple example. Some prerequisites of note:
- A PHP server (obvious, and I chose to use Zend's community server for testing)
- SSL on the Apache server (see this blog post on setting up a self-signed certificate)
- I'd recommend Eclipse PDT to help with source code creation and debugging
if (!Zend_Oauth_Client::$supportsRevisionA) {
$callback = $this->_consumer->getCallbackUrl();
if (!empty($callback)) {
//$params['oauth_callback'] = $callback;
}
}
And here's the PHP sample. Note that step 5 uses the BSS API, you could alternatively use the user API with the sample as well.
<?php
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Session.php';
Zend_Session::start();
// to understand OAuth process see
// http://www-10.lotus.com/ldd/appdevwiki.nsf/xpDocViewer.xsp?lookupName=
// IBM+Social+Business+Toolkit+documentation#action=openDocument&res_title
// =OAuth_1.0a_APIs_for_web_server_flow_sbt&content=pdcontent
// Smart Cloud URL (test server listed)
$sc4sbUrl = 'https://apps.na.collabservtest.lotus.com';
// vendor application (must be SSL for callback to work)
$callbackUrl = 'https://localhost/<replace with your file>.php';
// Step 1: Register the application
$consumerKey = '<replace with your key>';
$consumerSecret = '<replace with your secret>';
// check if a request token is available
// i.e. the current request to this page is the callback
if(!isset($_SESSION['SC4SB_REQUEST_TOKEN'])){
$step2config = array(
'callbackUrl' => 'http://localhost/SC4SBOAuth.php',
'requestTokenUrl' => $sc4sbUrl . '/manage/oauth/getRequestToken',
'accessTokenUrl' => $sc4sbUrl . '/manage/oauth/getAccessToken',
'authorizeUrl' => $sc4sbUrl . '/manage/oauth/authorizeToken',
'consumerKey' => $consumerKey,
'consumerSecret' => $consumerSecret,
'signatureMethod' => 'PLAINTEXT'
);
$consumer = new Zend_Oauth_Consumer($step2config);
// Step 2: Get a request token
$token = $consumer->getRequestToken();
// store the token for use in later steps
$_SESSION['SC4SB_REQUEST_TOKEN'] = serialize($token);
// Step 3: Obtain authorization
// redirect to the authorization endpoint
$consumer->redirect();
// the result is SC4SB returning to the callbackUrl
}
$step4n5config = array(
'requestTokenUrl' => $sc4sbUrl . '/manage/oauth/getRequestToken',
'accessTokenUrl' => $sc4sbUrl . '/manage/oauth/getAccessToken',
'authorizeUrl' => $sc4sbUrl . '/manage/oauth/authorizeToken',
'consumerKey' => $consumerKey,
'consumerSecret' => $consumerSecret,
'signatureMethod' => 'PLAINTEXT'
);
// check if an access token is available, if so, simply make the
// API call
if(!isset($_SESSION['SC4SB_ACCESS_TOKEN'])){
// Step 4: Get the access token
$consumer = new Zend_Oauth_Consumer($step4n5config);
// get the request token from the previous step
if (!empty($_GET) && isset($_SESSION['SC4SB_REQUEST_TOKEN'])) {
// now retrieve the access token from SC4SB
// e.g. oauth_token=fsf89fdssf9sdfsfsf0fdsfsf&oauth_token_secret=dhdsd99000fssfs89
$token = $consumer->getAccessToken(
$_GET,
unserialize($_SESSION['SC4SB_REQUEST_TOKEN'])
);
// store the access token for use in API calls
$_SESSION['SC4SB_ACCESS_TOKEN'] = serialize($token);
$_SESSION['SC4SB_REQUEST_TOKEN'] = null;
} else {
exit('Invalid callback request.');
}
}
// Step 5: Make the API call
$token = unserialize($_SESSION['SC4SB_ACCESS_TOKEN']);
// call a REST API
$client = $token->getHttpClient($step4n5config, $sc4sbUrl . '/api/bss/resource/customer');
$client->setMethod(Zend_Http_Client::GET);
$response = $client->request();
$result = $response->getBody();
print $result;
?>