API help

Manel Gimeno Zaragozá magiza83 at hotmail.com
Mon Jul 1 17:43:02 CEST 2013


Hello,

I'm noobie with php and that's why I have a lot of echo's and prints. I'm trying to develop some php scripts to call kolab api and I'm not sure what I'm doing wrong. You can find below the functions I've created. 

the auth function is working OK and I'm getting the session_id, but add_domain or list user are not working

<?php
function auth($auth_string){
        $auth_url = "http://192.168.1.1/kolab-webadmin/api/system.authenticate";
        $ch = curl_init($auth_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $auth_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($auth_string))
        );
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
                Propel::log(__CLASS__.'->'.__METHOD__.PHP_EOL.'Se ha producido un error llamando al webservice');
                curl_close($ch);
                return FALSE;
        }
        curl_close($ch);
        $parsedresponse = json_decode($result, TRUE);
        var_dump($parsedresponse);
        $session_id = $parsedresponse["result"]["session_token"];
        return $session_id;
}

function add_domain($auth_string,$domain){
        $auth_url = "http://192.168.1.1/kolab-webadmin/api/domain.add";
        $ch = curl_init($auth_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $domain);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-Session-Token: ' . $auth_string)
        );
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
                Propel::log(__CLASS__.'->'.__METHOD__.PHP_EOL.'Se ha producido un error llamando al webservice');
                curl_close($ch);
                return FALSE;
        }
        curl_close($ch);
        $parsedresponse = json_decode($result, TRUE);
        var_dump($parsedresponse);
        $response = $parsedresponse["status"];
        return $response;
}

$auth = array(
 "username" => "cn=admin",
 "password" => "pruebas");
$domain = "domain : pruebas.ddol.es";
$auth_string = json_encode($auth);
$id = auth($auth_string);
$info = add_domain($id,$domain);
echo "$info\n";
?>

I'm getting the following exit executing:
$ php add_domain.php
PHP Warning:  Module 'kolabformat' already loaded in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
array(2) {
  ["status"]=>
  string(2) "OK"
  ["result"]=>
  array(4) {
    ["user"]=>
    string(12) "cn=admin"
    ["userid"]=>
    string(12) "cn=admin"
    ["domain"]=>
    string(13) "kolab.ddol.es"
    ["session_token"]=>
    string(26) "obpl65g8tj0isp0eh10dojcdv5"
  }
}
array(2) {
  ["status"]=>
  string(2) "OK"
  ["result"]=>
  array(0) {
  }
}
OK

But nothing is being added the LDAP.

As I said before user list i not working either:

function user_info($auth_string,$user){
        $auth_url = "http://192.168.1.1/kolab-webadmin/api/user.info";
        $auth_url .= '?' . "$user";
        echo "$auth_url\n";
        print_r(parse_url($auth_url));
        $ch = curl_init($auth_url);
        #curl_setopt($ch, CURLOPT_URL, $auth_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-Session-Token: ' . $auth_string)
        );
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
                Propel::log(__CLASS__.'->'.__METHOD__.PHP_EOL.'Se ha producido un error llamando al webservice');
                curl_close($ch);
                return FALSE;
        }
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        var_dump($status);
        curl_close($ch);
        $parsedresponse = json_decode($result, TRUE);
        var_dump($parsedresponse);
        $response = $parsedresponse["status"];
        return $response;
}


$auth = array(
 "username" => "user at kolab.ddol.es",
 "password" => "test");

$user = "user=user at kolab.ddol.es";

$auth_string = json_encode($auth);
$id = auth($auth_string);
$info = user_info($id,$user);
echo "$info\n";

and the exit is:

$ php list_user.php
PHP Warning:  Module 'kolabformat' already loaded in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
PHP Notice:  Constant swig_runtime_data_type_pointer already defined in Unknown on line 0
array(2) {
  ["status"]=>
  string(2) "OK"
  ["result"]=>
  array(4) {
    ["user"]=>
    string(7) "user"
    ["userid"]=>
    string(44) "uid=user,ou=People,dc=kolab,dc=ddol,dc=es"
    ["domain"]=>
    string(13) "kolab.ddol.es"
    ["session_token"]=>
    string(26) "surkjn7tqnrobeoio65dofo7r0"
  }
}
http://192.168.1.1/kolab-webadmin/api/user.info?user=user@kolab.ddol.es
Array
(
    [scheme] => http
    [host] => 192.168.1.1
    [path] => /kolab-webadmin/api/user.info
    [query] => user=user at kolab.ddol.es
)
int(200)
array(3) {
  ["status"]=>
  string(5) "ERROR"
  ["code"]=>
  int(500)
  ["reason"]=>
  string(14) "Internal error"
}
ERROR

I don't know if I'm not using correctly the api nor passing correctly the parameters.

Thanks for your help.

Manel
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kolab.org/pipermail/users/attachments/20130701/e24cb314/attachment.html>


More information about the users mailing list