尝试将新用户添加到Jasperserver时,PHP REST API出错

时间:2013-11-29 01:29:38

标签: php jasper-reports jasperserver

我遇到了php-sample的问题。 我想添加用户,但出现了问题。 请帮我!! 这是我使用的代码:

<!DOCTYPE html>
<?php
    require_once "vendor/autoload.php";
    require_once "autoload.dist.php";
    require_once "client/JasperClient.php";
    require_once "client/User.php";
    require_once "client/Role.php";
    $client = new Jasper\JasperClient(
                    "localhost", // Hostname
                    8080, // Port
                    "jasperadmin", // Username
                    "jasperadmin", // Password
                    "/jasperserver-pro", // Base URL
                    "organization_1"
                ); // Organization (pro only)

    $newUser = new Jasper\User("BI_User",                // username
           "superSTRENGTHpassw0rd",      // password
           "clever@email.com",           // email
           "Business Intelligence User", // description
           "organization_1",             // parent organization
           "true"                        // enabled
           );

   $role = new Jasper\Role("ROLE_USER", NULL, "false");
   $newUser->addRole($role);

   try {
      $client->putUsers($newUser);
  }
  catch (Exception $e) {
      printf("Could not add new user: %s", $e->getMessage());
  }?>

以下是我收到的错误消息:

Could not add new user: Unexpected HTTP code returned: 400 Body of response: 
Apache Tomcat/6.0.26 - Error report HTTP Status 400 - type Status 
reportmessage description The request sent by the client was syntactically 
incorrect ().Apache Tomcat/6.0.26

1 个答案:

答案 0 :(得分:1)

问我花时间谷歌这么多问题,我找到了解决方案。 这是我的解决方案是否有人感兴趣。

<?php
require_once "vendor/autoload.php";
require_once "autoload.dist.php";

use Jaspersoft\Client\Client;
use Jaspersoft\Dto\User\User;
use Jaspersoft\Dto\Role\Role;


function registerUsers(){
    $client = new Client(
            "localhost",
            "8080",
            "superuser",
            "superuser",
            "/jasperserver-pro",
            "null"
       );
    $file_path = 'data/userlist.csv';
    $handle = fopen($file_path,'r');
    $array_users = array();
    $i=0;
    while (!feof($handle) !==false){
        $line = fgetcsv($handle,1024,',');
        $i++;
        if($i==1) continue;
        if(!empty($line)){
            for($c = 0; $c < count($line); $c++){
                $username = $line[0];
                $password = $line[1];
                $email = $line[2];
                $fullname = $line[3];
                $tenantId = $line[4];
                $enabled = $line[5];
                $user = new User($username, $password, $email, 
                                $fullname, $tenantId, $enabled);
                $role = new Role('ROLE_USER', null, 'false');
                $array_users[$c] = $user;
                $array_users[$c]->addRole($role);
                try {
                    $client->userService()->addUsers($array_users[$c]);
                } catch (Exception $e) {
                    printf('Could not add new user: %s', $e->getMessage());
                }
            }
        }
    }
}?>

这是我的csv数据文件:

User Name,Password,Email Address,Full Name,Tenant ID,Enable
User1,superSTRENGTHpassw0rd,clever@email.com,User One,a,true
User2,superSTRENGTHpassw0rd,clever@email.com,User One,a,true
User3,superSTRENGTHpassw0rd,clever@email.com,User One,a,true
User6,superSTRENGTHpassw0rd,clever@email.com,User One,organization_1,true
User7,superSTRENGTHpassw0rd,clever@email.com,User One,organization_1,true
User8,superSTRENGTHpassw0rd,clever@email.com,User One,b,true
User9,superSTRENGTHpassw0rd,clever@email.com,User One,b,true
User10,superSTRENGTHpassw0rd,clever@email.com,User One,c,true
User11,superSTRENGTHpassw0rd,clever@email.com,User One,c,true