PHP SOAP GET USER DATA

English! place to talk about OTRS development, programming and coding
Post Reply
heck
OTRS newbie
Posts: 4
Joined: 14 Sep 2010, 17:13
OTRS Version?: 2.4

PHP SOAP GET USER DATA

Post by heck » 20 Jan 2011, 00:29

Hi,
I do have a (basic) questions which is related to php and the handling of arrays:

I am writing a php function with the input "UserID" and the output should be"UserEmail" as an output.

From the API I am using and the output is an ARRAY:

Code: Select all

GetUserData()
    get user data (UserLogin, UserFirstname, UserLastname, UserEmail, ...) 

        my %User = $UserObject->GetUserData(
            UserID => 123,
            Cached => 1, # not required -> 0|1 (default 0)
        );
My php code is this:

Code: Select all

<?PHP

$user = "xxx";
$pass = "xxx";

$client = new SoapClient(null, array('location'  => "http://xxx.xxx.xx/otrs/rpc.pl",
                                     'uri'       => "Core",
                                     'trace'     => 1,
                                     'login'     => $user,
                                     'password'  => $pass,
                                     'style'     => SOAP_RPC,
                                     'use'       => SOAP_ENCODED));
try {

$result = $client->__soapCall("Dispatch", array($user, $pass, "UserObject", "GetUserData", "UserID" , 2));
} catch (SoapFault $fault) {
  echo "REQUEST:\n".$client->__getLastRequest()."\n";
  echo "RESPONSE:\n".$client->__getLastResponse()."\n";
  echo "</pre>";
  exit;
}

#echo "REQUEST:\n".$client->__getLastRequest()."\n";
echo "RESPONSE:\n".$client->__getLastResponse()."\n\n";

var_dump($result);

?>
and the var_dump result shows this (only the first 20 lines):

Code: Select all

RESPONSE:
OutOfOffice0ValidID1UserRefreshTime0UserCommentOutOfOfficeStartMonth1UserEmailx.xxx@xxx.xxUserTicketOverviewAgentTicketQueuePreviewUserThemeStandardUserID2.......................
array
  's-gensym3' => string 'OutOfOffice' (length=11)
  's-gensym5' => int 0
  's-gensym7' => string 'ValidID' (length=7)
  's-gensym9' => int 1
  's-gensym11' => string 'UserRefreshTime' (length=15)
  's-gensym13' => int 0
  's-gensym15' => string 'UserComment' (length=11)
  's-gensym17' => string '' (length=0)
  's-gensym19' => string 'OutOfOfficeStartMonth' (length=21)
  's-gensym21' => int 1
  's-gensym23' => string 'UserEmail' (length=9)
  's-gensym25' => string 'x.xxx@xxx.xx' (length=21)
  's-gensym27' => string 'UserTicketOverviewAgentTicketQueue' (length=34)
  's-gensym29' => string 'Preview' (length=7)
  's-gensym31' => string 'UserTheme' (length=9)
  's-gensym33' => string 'Standard' (length=8)
  's-gensym35' => string 'UserID' (length=6)
  's-gensym37' => int 2
............
Now how can I get the value of 'UserEmail' into a variable $UserEmail ??
So perhaps somebody is out there who can give a hint to solve this basic problem.
Thank you!

crythias
Moderator
Posts: 10109
Joined: 04 May 2010, 18:38
OTRS Version?: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: PHP SOAP GET USER DATA

Post by crythias » 21 Jan 2011, 15:36

It's not perfectly easy to do that, because the data is stored in the table as Key in one array entry, and Value in the next entry.

But assuming all's the same

$UserEmail= $result["s-gensym25"];

But just for you.
OTRS 5.0.x (private/testing/public) on Linux with MySQL database. Also on github.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask

Andre Bauer
OTRS guru
Posts: 2191
Joined: 08 Dec 2005, 17:01
OTRS Version?: 5.0.x
Real Name: André Bauer
Company: Magix Software GmbH
Location: Dresden
Contact:

Re: PHP SOAP GET USER DATA

Post by Andre Bauer » 21 Jan 2011, 22:28

Just create a new array which is easier to read.

I did that some time ago:

Code: Select all

<?php
/*
    OTRS SOAP
*/

# otrs soap config
$soap_url = "http://localhost/otrs/rpc.pl";
$soap_user = "some_user";
$soap_pass = "some_pass";

// new soap connection to otrs
$client = new SoapClient(null, array(
    'location'  => $soap_url,
    'uri'       => "Core",
    'trace'     => 1,
    'login'     => $soap_user,
    'password'  => $soap_pass,
    'style'     => SOAP_RPC,
    'use'       => SOAP_ENCODED));

echo "<pre>";

// get queues
$queues = $client->__soapCall("Dispatch", array($soap_user, $soap_pass,
    "QueueObject", "GetAllQueues"
));

// create new array from data values
$values = array_values($queues);
$key_start = 0;
$key_end = count($values)-1;

// create new asoc array with key & values
$queue_array = array();
for ($key = $key_start; $key < $key_end; $key++) {
    $value = $key + 1;
    $queue_array[$values[$key]] = $values[$value];
    $key++;
}

// get data from new queue array
print_r($queue_array);

echo "</pre>";

?>
Should work for other data the same way...
Prod: Ubuntu Server 16.04 / Zammad 1.2

DO NOT PM ME WITH OTRS RELATED QUESTIONS! ASK IN THE FORUMS!

OtterHub.org

heck
OTRS newbie
Posts: 4
Joined: 14 Sep 2010, 17:13
OTRS Version?: 2.4

Re: PHP SOAP GET USER DATA [SOLVED]

Post by heck » 24 Jan 2011, 16:27

It is perfect example. I adopted it to my needs.
Thank you!

Post Reply