10 Replies Latest reply: Aug 1, 2014 9:10 AM by Blake Grotewold RSS

    QlikView management API and PHP

      Okay so I have been scratching my head for a while on this problem and would appreciate any help / guidance I can get.  So this is the problem:  Basically QlikView server management 11 release API in the method of SOAP.  So what I am trying to do is to write a simple php script(to learn!) that uses one of the SOAP operation.  I am trying to get the limited service key by using the GetTimeLimitedServiceKey, but unfortunately, when I run my script I get the following error: "Unauthorized".  Has anyone been able to write a simple script in PHP that uses the management API?

       

      This is my simple code below:

      //----------------------------------------------------------------------------------------------------------------

       

      <?php

       

          try {

                  $options = array(

                      'soap_version'=>SOAP_1_1,

                      'exceptions'=>true,

                      'trace'=>1,

                      'cache_wsdl'=>WSDL_CACHE_NONE,

                      'login'=>"username",

                      'password'=> "*******"

                  );

                  $client = new SoapClient('http://qlikviewserver:4799/QMS/Service?WSDL', $options);

       

      // Note where 'Get' and 'request' tags are in the XML

                  $results = $client->GetTimeLimitedServiceKey();

               } catch (Exception $e) {

                  echo "<h2>Exception Error!</h2>";

                  echo $e->getMessage();

              }

       

      $results = $client->GetTimeLimitedServiceKey();

       

      ?>

       

      Thanks.

        • Re: QlikView management API and PHP

          Hi Samuel,

           

          Yes, I use PHP to manage my Qlikview 10 server.

           

          Your piece of code doesn't work because Qlikview uses the NTLM protocol.

           

          You need to redefine the __doRequest() method like above :

          class NTLMSoapClient extends SoapClient

          {

               public $serviceKey = null;

           

               function __doRequest($request, $location, $action, $version, $one_way = 0)

               {

                    $headers = array(

                         'Method: POST',

                         'Connection: Keep-Alive',

                         'User-Agent: PHP-SOAP-CURL',

                         'Content-Type: text/xml; charset=utf-8;',

                         'SOAPAction: '.$action

                    );

                    //Once you have a service key

                    if ($this->serviceKey)

                         $headers[] = 'X-Service-Key: ' . $this->serviceKey;

           

                    $this->__last_request_headers = $headers;

                    $ch = curl_init($location);

                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

                    curl_setopt($ch, CURLOPT_POST, true);

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

                    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

                    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);

                    curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASSWORD');

                    $response = curl_exec($ch);

           

                    return $response;

               }

           

               function __getLastRequestHeaders() {

                    return implode("\n", $this->__last_request_headers)."\n";

               }

          }

           

          You will have to edit the 'USER:PASSWORD' with yours, the user has to be in "Qlikview Administrators" AND "Qlikview Management API" group

           

          And a piece of code to use it :

           

          $options = array(

               'exceptions'=>true,

               'trace'=>1

          );

           

          //Create SOAP connexion

          $client = new NTLMSoapClient('http://localhost:4799/QMS', $options);

           

          //Retrieve a time limited service key

          $client->serviceKey = $client->GetTimeLimitedServiceKey()->GetTimeLimitedServiceKeyResult;

           

          //Retrieve server info

          $qlikviewServerInfo = $client->GetServices(array('serviceTypes' => 'QlikViewServer'))->GetServicesResult;

           

          echo 'QVS ID: ' . $qlikviewServerInfo->ServiceInfo->ID;

           

           

          I hope it will help ! The technical documentation is the worst I've ever seen !!