This manual is deprecated. Please visit https://groupoffice.readthedocs.io for the latest documentation.

Difference between revisions of "API calls"

From Group-Office Groupware and CRM Documentation
Jump to: navigation, search
(Logging in)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Logging in==
 
==Logging in==
You can login to Group-Office using http calls. Here's a PHP CURL example:
+
Here's some sample code on how to login to Group-Office with the CURL based HTTP client and get notes from the demo.
 
<pre>
 
<pre>
 +
 
<?php
 
<?php
  
$tmpdir = '/tmp';
+
//Adjust this path to suit your needs
$baseUrl = 'http://localhost/groupoffice/index.php';
+
require('/usr/share/groupoffice/GO.php');
$username = 'admin';
+
$password = 'admin';
+
  
$ch = curl_init();
+
//not required but can be used to get CLI input params
 +
$params = GO_Base_Util_Cli::parseArgs();
  
 +
$host = 'http://demo.group-office.eu/';
  
$cookieFile = $tmpdir . '/cookie.txt';
+
//Login
 +
$httpClient = new GO_Base_Util_HttpClient();
 +
$success = $httpClient->groupofficeLogin($host, 'demo','demo');
  
 +
if(!$success)
 +
        throw new Exception("Could not login to Group-Office");
  
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
+
//Get notes
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
+
$response = $httpClient->request($host,array(
 +
                'r'=>'notes/note/store',
 +
      'start'=>0,
 +
      'limit'=>10
  
//for self-signed certificates
+
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
+
  
 +
$result = json_decode($response);
 +
//print names
 +
foreach($result->results as $record){
 +
  echo $record->name."<br>\n";
 +
}
 +
</pre>
  
$postfields = array(
+
==Browser autologin script==
'username' => $username,
+
'password' => $password
+
);
+
  
 +
If you want to create some kind os single sign on solution in the browser, this script may get you started:
 +
<pre>
 +
<?php
 +
require('/path/to/GO.php');
  
curl_setopt($ch, CURLOPT_URL,$baseUrl . '?r=auth/login');
+
$user='Get the username in a very secure way!';
curl_setopt($ch, CURLOPT_POST, true);
+
$pass='Get the password in a very secure way!';
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
+
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest"));
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
  
$response = curl_exec($ch);
+
GO::session()->login($user, $pass);
+
$error = curl_error($ch);
+
if(!empty($error))
+
die("curl error: ".$error);
+
 
+
$response = json_decode($response, true);
+
 
+
var_dump($response);
+
  
 +
header('Location: /url/to/groupoffice');
 
</pre>
 
</pre>
 
Running this script should return something like:
 
 
<pre>
 
array(4) {
 
  ["success"]=>
 
  bool(true)
 
  ["user_id"]=>
 
  string(1) "1"
 
  ["security_token"]=>
 
  string(20) "nbhr9pmksfvg4czwjldq"
 
  ["sid"]=>
 
  string(26) "eiijnl8iupkk345qpnhaukvml6"
 
}
 
</pre>
 
 
The security token must be send as parameter to each request. Learn more about controller actions on the [[Controllers]] page.
 

Latest revision as of 16:42, 28 August 2013

Logging in

Here's some sample code on how to login to Group-Office with the CURL based HTTP client and get notes from the demo.


<?php

//Adjust this path to suit your needs
require('/usr/share/groupoffice/GO.php');

//not required but can be used to get CLI input params
$params = GO_Base_Util_Cli::parseArgs();

$host = 'http://demo.group-office.eu/';

//Login
$httpClient = new GO_Base_Util_HttpClient();
$success = $httpClient->groupofficeLogin($host, 'demo','demo');

if(!$success)
        throw new Exception("Could not login to Group-Office");

//Get notes
$response = $httpClient->request($host,array(
                'r'=>'notes/note/store',
      'start'=>0,
      'limit'=>10

));

$result = json_decode($response);
//print names
foreach($result->results as $record){
   echo $record->name."<br>\n";
}

Browser autologin script

If you want to create some kind os single sign on solution in the browser, this script may get you started:

<?php
require('/path/to/GO.php');

$user='Get the username in a very secure way!';
$pass='Get the password in a very secure way!';

GO::session()->login($user, $pass);

header('Location: /url/to/groupoffice');