...
Expand |
---|
title | Example: JSON call with http authentication using PHP and Zend Framework |
---|
|
Code Block |
---|
| <?php
/* Example requires Zend Framework */
require_once('Zend/Loader/Autoloader.php');
function serviceLayerCall($method, $params=null)
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$url="http://EFSINSTALLATIONURL/service/index.php?handler=json&token=TOKEN";
$client = new Zend_Http_Client();
$client->setUri($url);
$client->setConfig(array('timeout' => 30));
$client->setMethod(Zend_Http_Client::POST);
/* not using TOKEN? Here is basic auth: */
//$user = "USER";
//$passwd = "PASSWORD";
//$client->setAuth($user, $passwd);
$client->setRawData(json_encode(array('method' => $method, 'jsonrpc' => '2.0', 'id' => 1, 'params' => $params)));
$request = $client->request();
//print "<pre>";
//var_dump($request->getBody());
$return = json_decode($request->getBody());
$return=$return->result;
return $return->return;
}
/* 1. Step - Get projects
*/
print "<h3>1. step - get projects</h3>";
$projects=serviceLayerCall("survey.surveys.getList");
foreach($projects as $project)
{
print "- ".$project->title." (".$project->id.")<br>";
}
//2. Step - Get Structure for Pid 1112
, first a helper function:
function sub($pages, &$myquestions)
{
foreach($pages as $page)
{
if(count($page->subPages) > 0)
$this->sub($page->subPages, $myquestions);
foreach($page->questions as $question)
{
$myquestions[]=$question;
}
}
return $myquestions;
}
$relevantVars=array();
print "<hr><h3>2. step - get structure for Pid 1112</h3>";
$project=serviceLayerCall("survey.surveys.getQuestionnaireStructure", array("surveyId" => 1112));
$questions=sub($project, $myquestions);
foreach($questions as $question)
{
if($question->questiontext) {
print $question->questiontext . "<br>";
foreach ($question->variables as $variable) {
if ($variable->type == "char" || $variable->type == "text") {
print "Text-Var: ".$variable->varname . "<br>";
//add to $relevantVars for export
$relevantVars[] = $variable->varname;
}
}
}
}
/* 3 - Relevant vars for export */
print "<hr><h3>3. Collected variables for export:</h3>";
var_dump($relevantVars);
print "</pre>";
/* 4 - Get CSV export */
print "<hr><h3>4. Get results for specific vars</h3>";
$exportData=serviceLayerCall("survey.results.getRawdataCSV", array("surveyId" => 1112, "exportTypes" => array("QUESTIONNAIRE"), "includeVariables" => $relevantVars));
print base64_decode($exportData); |
|
Info |
---|
The full EFS Service-Layer service layer call overview provides example REST requests and responses. |
Exploring available SOAP and REST services using 3rd party tools
You can easily get familiar with our EFS Service-Layer, by using 3rd party REST or SOAP clients. Two of such tools are SoapUI for SOAP requests and Postman for REST.
Expand |
---|
|
You need to have the full URL to the WSDL description, as described above, and the token. Basic authentication is also possible, the client will ask you to provide login information automatically. In this example we use https://my-efs/service/?handler=soap&wsdl=1&token=1234567890 . Image AddedSoapUI will read the description and create example SOAP requests for all available services. Double-clicking on a request will open the following window, click on the green execute button to submit the request and a response will be shown. Image Added |
Expand |
---|
|
You will need to download the RAML file (https://my-efs/service/?handler=rest&raml=1&token=1234567890 ) and import it as a collection in Postman. Image AddedThe collections tab will now have a list of all available services on the EFS installation. Info |
---|
Instead of importing all services from RAML, you can also create single requests by clicking on “New” and selecting “Request”. | Image Added |