Clarity

 View Only
Expand all | Collapse all

SOAP Web Service Access to Clarity PPM

Jump to Best Answer
  • 1.  SOAP Web Service Access to Clarity PPM

    Posted Jun 23, 2011 08:38 PM
    Hi,

    I am trying to connect to Clarity through PHP soapClient. I am able to connect to the login function and get a session id back but can't get a query to work. Here is some test code I've been working on. Hopefully someone has already solved this problem and can give me some pointers. Thanks in advance for any suggestions or help.


    This code returns a session ID.

    $client = new SoapClient("https://******.myclarity.com/niku/wsdl/Query/***_inv_classes");
    $session_id = ($client->Login(array('Username'=>"xxxxx", 'Password'=>"******")));
    echo $session_id;


    This code does not work whether I use the session id from the above query or pass it the user/password as I've show here.

    $client = new SoapClient("https://xxxxx.myclarity.com/niku/wsdl/Query/***_inv_classes", array('Username'=>"******", 'Password'=>"******"));
    try {$results = ($client->Query(array('Code'=>"xxxxx_inv_classes")));
    echo count($results);
    }
    catch (Exception $e) {}


    Using SoapSonar I can see what a well formed SOAP request looks like. The user/pass goes into the header while the query information goes into the body. I've tried a bunch of combinations to get the right things into the right places but so far no luck.

    Thanks. - Justin


  • 2.  RE: SOAP Web Service Access to Clarity PPM

     
    Posted Jun 27, 2011 12:14 PM
    Hi All,

    Anyone have any ideas here for Justin?

    Thanks!
    Chris


  • 3.  RE: SOAP Web Service Access to Clarity PPM

    Posted Apr 30, 2013 07:16 AM
    Hi all,

    I am facing the same problem - can anyone provide a working example on how to use PHP and soap (e.g. nusoap) library?
    With the attached code I do get a session id, but have no clue on how to query e.g. some report or get the project details.
    Currently I only get the error message:
    No active session.  Please login or provide a valid session identifier.
    Thanks, Till
    <?
    // only errors ...
    error_reporting(E_ERROR | E_PARSE);
    
    
    require_once('nusoap/lib/nusoap.php');
    
    
    
    // 
    class SPaRCExtract {
    
    
    var $client;
    
    var $activesession;
    
    var $username;
    
    var $password;
    
    
    function logon($username,$password) {
    
    
    $this->username = $username;
    
    
    $this->password = $password;
    
    
    $this->debug("logging on with $username and $password");
    
    
    $this->client = new nusoap_client("http://www.ourhostname.com/niku/wsdl/Query/R123?tenantId=clarity", true);
    
    
    $err = $this->client->getError();
    
    
    $args = array('Username' => $username, 'Password' => $password);
    
    
    $return = $this->client->call('WrappedLogin', array($args));
    
    
    $error = $this->client->getError();
    
    
    
    $this->debug("Logon - Client error: " . $error);
    
    
    $this->debug("Logon - Client responds" . $return);
    
    
    // @todo - errorhandling
    
    
    $this->activesession = $return;
    
    }
    
    
    function query($query) {
    
    
    $auth = array('SessionID' => $this->activesession);
    
    
    $args = array(auth => $auth);
    
    
    var_dump($args);
    
    
    $return = $this->client->call('Query', array($args));
    
    
    $error = $this->client->getError();
    
    
    
    $this->debug("Query - Client error: " . $error);
    
    
    $this->debug("Query - Client responds:" . $return);
    
    
    
    
    
    
    
    var_dump ($return);
    
    }
    
    
    
    function debug($text) {
    
    
    echo $text . "\n";
    
    }
    }
    
    
    $sparcextract = new SPaRCExtract ();
    $sparcextract->logon("username","password");
    $sparcextract->query();
    
    
    ?>


  • 4.  RE: SOAP Web Service Access to Clarity PPM
    Best Answer

    Posted Aug 02, 2013 06:35 PM
    I know this was a while ago but here is a PHP script that performs a query. It ran in our 13.1 environment using PHP 5.3.

    V/r,
    Gene

    <?php

    error_reporting(E_ERROR | E_PARSE);

    require_once('nusoap.php');

    class SPaRCExtract {

    var $client;
    var $activesession;
    var $username;
    var $password;

    function logon($username,$password) {
    $this->username = $username;
    $this->password = $password;



    $this->debug("logging on with $username and $password");


    // set the client to the NSQL query wsdl
    $this->client = new nusoap_client("http://www.ourhostname.com/niku/wsdl/Query/MyNSQLQuery?wsdl", TRUE);
    $err = $this->client->getError();
    $args = array('Username' => $username, 'Password' => $password);
    $return = $this->client->call('WrappedLogin', array($args));
    $error = $this->client->getError();
    $this->debug("Logon - Client error: " . $error);
    $this->debug("Logon - Client responds" . $return);
    // @todo - errorhandling
    $this->activesession = $return;
    }

    function query() {
    $args = array('Auth' => $this->activesession); //add active session to the Auth
    var_dump($args);
    $query = array('Query' => ''); // we are doing a query without any filters
    $return = $this->client->call('Query', $query, null, null, $args); // call the query method with empty query params and load the header with the Auth
    $error = $this->client->getError();
    $this->debug("Query - Client error: " . $error);
    $this->debug("Query - Client responds:" . $return);
    var_dump ($return);
    }

    function debug($text) {
    echo $text . "\n";
    }
    }

    $sparcextract = new SPaRCExtract ();
    $sparcextract->logon("username","password");
    $sparcextract->query();

    ?>


  • 5.  Re: SOAP Web Service Access to Clarity PPM

    Posted Oct 28, 2014 03:46 PM

    My friend,

     

    I tried to do a query using filters but i can't. How can I do this?

     

    I refer to this code snippet:

    $query = array('Query' => ''); // we are doing a query without any filters

    $return = $this->client->call('Query', $query, null, null, $args); // call the query method with empty query params and load the header with the Auth

     

    Please help!

     

    ***.

    Lucas



  • 6.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 09, 2016 05:18 PM

    First, kudos to Gene Greiff for the original correct answer.

     

    However, did anyone ever get this working with a filtered query? I've been bashing my head against the wall with this for hours, and no matter what I do, I can't get the results of a filtered query. I can login and retrieve all results, but not with filters. I'm simply trying to retrieve the most basic project data (name, id, status, etc) for a single project-- maybe 6 fields total.

     

    I've got SOAPUI returning the record properly, but nothing works with soapclient or nusoap for for that matter.

     

    This is the only thread that pops up on google that is even remotely relevant.

     

    Any hints or suggestions would be most welcome.



  • 7.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 09, 2016 06:34 PM

    Take a look at this.

     

    GitHub - wsdl2phpgenerator/wsdl2phpgenerator-cli: wsdl2phpgenerator command line application *work-in-progress*

     

    It will generate all the PHP classes that you will need for a given WSDL.

     

    I just ran it against one of my queries and it output the familiar classes that you would see with wsdl2java.

     

    V/r,

    Gene



  • 8.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 03:53 AM

    Adding a filter to a SOAP query is just a matter of adding the <filter> section to the XML;

     

    (Sorry, I can only really explain in XML terms, upto you how you construct that XML in whatever client you are calling the webservice with, hopefully you can spot what you need to do from this)

     

    So an unfiltered query might look like;

     

    <Query xmlns="http://www.niku.com/xog/Query">

    <Code>my_ppm_query_q</Code>

    </Query>

     

    and a filtered, sorted one like this;

     

    <Query xmlns="http://www.niku.com/xog/Query">

    <Code>my_ppm_query_q</Code>

    <Filter>

    <param_obs_type>N2K_EH</param_obs_type>

    </Filter>

    <Sort>

      <Column>

        <Name>obs_name</Name>

        <Direction>asc</Direction>

      </Column>

    </Sort>

    </Query>

     

    (obviously the detail of "my_ppm_query_q" is not relevant, filter fields and returned fields just used for illustration)

     

    You can also add "power filters" using same sort of syntax you would in the Clarity UI, e.g. ;

     

    <FilterExpression>(status!='COMPLETED' || status == '')</FilterExpression>

     

    (that FilterExpression is a separate tag, not part of the Filter tag)

     

    EDIT : and the syntax of the FilterExpression required will depend on your query, it has to be SQL-syntax (rather than NSQL-syntz) if you are calling a query, and application-like if calling an object read)



  • 9.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 11:27 AM

    Adding a filter to a SOAP query is just a matter of adding the <filter> section to the XML;

    I totally get that. And that's why I did my troubleshooting/prototyping with SOAPUI.

     

    The XML that works perfectly via SOAP UI is this:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://www.niku.com/xog/Query">

      <soapenv:Header>

          <quer:Auth>

            <quer:Username>username</quer:Username>

            <quer:Password>password</quer:Password>

          </quer:Auth>

      </soapenv:Header>

      <soapenv:Body>

          <quer:Query>

            <quer:Code>***.yyy.prjReview</quer:Code>

            <quer:Filter>

                <quer:project_int_id>1234567</quer:project_int_id>

            </quer:Filter>

          </quer:Query>

      </soapenv:Body>

    </soapenv:Envelope>

     

    And the native soapclient PHP code I've been working with is:

     

    $url = "http://somedomain.com/niku/wsdl/Query/***.yyy.prjReview";

    $auth = array('Username' => 'username', 'Password' => 'password');

    $client = new SoapClient($url, array("trace" => 1, "exception" => 0));

     

    $header = new SOAPHeader('http://www.niku.com/xog/Query', 'Auth', $auth);

    $client->__setSoapHeaders($header);

     

    $query = array(

      'Query' => array(

        'Code' => '***.yyy.prjReview',

        'Filter' => array('project_int_id' => '1234567'),

      ),

    );

    try {

      $result = $client->__soapCall('Query', $query);

      var_dump($result);

    } catch (SoapFault $fault) {

      echo ("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})");

    }

     

    Which produces this XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.niku.com/xog/Query">
      
    <SOAP-ENV:Header>
        
    <ns1:Auth>
           <ns1:Username>username</ns1:Username>
          
    <ns1:Password>password</ns1:Password>
      
    </ns1:Auth>
      
    </SOAP-ENV:Header>
        
    <SOAP-ENV:Body>
          
    <ns1:Query>
            
    <ns1:Code>***.yyy.prjReview</ns1:Code>
            
    <ns1:Filter>
              
    <ns1:project_int_id>1234567</ns1:project_int_id>
            
    </ns1:Filter>
        
    </ns1:Query>
      
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

     

    It logs in fine, but appears to be ignoring the filter and simply returning all the results for that query.



  • 10.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 11:43 AM

    OK I don't immediately see anything "wrong" with any of that - if you getting the "expected results" with the SOAPUI call then that's about where my understanding ends (I can't read that PHP code at all sorry).



  • 11.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 12:02 PM

    I JUST FIGURED IT OUT and OMG what a moment.

     

    <query /> appears in the XML without inserting it explicitly into the parameters array-- so I just had one extra level in the array. The correct parameters that now work are:

     

    $query = array(

      'Code' => '***.yyy.prjReview',

      'Filter' => array('project_int_id' => '1234567'),

    );

     

    The filter is now working as expected.

     

    Hopefully this saves someone else hours and hours of head banging, lol.

     

    Thanks for all the replies everyone!



  • 12.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 12:02 PM

    So i just  tested your PHP code against my dev instance 14.3.

     

    <?php
    /**
     * Created by PhpStorm.
     * User: ggreiff
     * Date: 3/10/2016
     * Time: 8:52 AM
     */
    $url = "https://cppm.ondemand.ca.com/niku/wsdl/query/mhs_users_license";
    $auth = array('Username' => 'admin', 'Password' => 'password');
    $client = new SoapClient($url, array("trace" => 1, "exception" => 0));
    $header = new SOAPHeader('http://www.niku.com/xog/Query', 'Auth', $auth);
    $client->__setSoapHeaders($header);
    
    $query = array(
        'Query' => array(
            'Code' => 'mhs_users_license',
            'Filter' => array('id' => 'ggreiff'),
        ),
    );
    
    try {
        $result = $client->__soapCall('Query', $query);
        var_dump($result);
    } catch (SoapFault $fault) {
        echo("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})");
    }
    

     

    And filter works -- I only get one record back.

     

     

    V/r,

    Gene



  • 13.  Re: SOAP Web Service Access to Clarity PPM

    Posted Mar 10, 2016 12:03 PM

    I got it working also-- see my reply above. No clue why my original $query works for you but not for me though.

     

    And thank you SO VERY MUCH for testing-- sometimes just having someone else test to know you're not losing your mind is an credibly helpful reply!