Search code examples
phprestcurlpostmanalm

ALM SaaS REST API 12.50 not working in PHP


I have tried below code in PHP to get the defect details from ALM but its not showing any response in browser. But the same is working in POSTMAN . Can someone help me here

Here is the document of REST API USAGE REST DOCUMENT FROM ALM

I have already tried existing posts from Stackoverflow

  1. HP ALM REST API login using PHP CURL
  2. ALM REST API v12.50 error 401

Nothing is helping so posted a new question

Note : Due to security purpose header value is kept as encoded value

 <?php

    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://hostname/qcbin/api/domains/domainname/projects/projectname/defects/?limit=10",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic encoded value",
        "cache-control: no-cache",
        "postman-token: a8a2398d-7a0a-0ebd-a586-58a40e524a9a"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
?>

Solution

  • I have finally found the solution and below is the approach

    First we need get to LWSSO_COOKIE_KEY,QCSession,ALM_USER,XSRF_TOKEN values from the ALM Authentication link then we should use the values for subsequent calls

    Below is the complete working code to get the list of defects by entering ALM Credentials

            <?php
            $curl = curl_init();
            Header('Content-type: application/json');
            $credentials = "username:password";
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://host:port/qcbin/api/authentication/sign-in",
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HEADER => 1,
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "authorization: Basic " . base64_encode($credentials) ,
                    "cache-control: no-cache"
                ) ,
            ));
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
    
            if ($err)
                {
                echo "cURL Error #:" . $err;
                }
              else
                {
    
                // If there is no error then get the response to form the array of headers to get the different values required
    
                $array_start = explode(';', $response);
                foreach ($array_start as $key => $value) {
        $remove_from_string = ['HTTP/1.1 200 OK','Path=/','HTTPOnly','HttpOnly','Content-Length',': 0'];
        $replace_array = ['','','','','',''];
        $value = str_replace($remove_from_string,$replace_array,$value);
        $value = trim(preg_replace(('/Expires: [a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ [a-zA-Z]+/'), '', $value));
        $value = trim(preg_replace(('/Server: [a-zA-Z0-9.\(\)]+/'),'',$value));
        if (!empty($value)) {
            $almheaders[trim(explode('=',$value)[0])] = explode('=',$value)[1];
        }
    }
                $LWSSO_COOKIE_KEY = $almheaders['Set-Cookie: LWSSO_COOKIE_KEY'];
                $QCSession = $almheaders['Set-Cookie: QCSession'];
                $ALM_USER = $almheaders['Set-Cookie: ALM_USER'];
                $XSRF_TOKEN = $almheaders['Set-Cookie: XSRF-TOKEN'];
    
                // Now form the Cookie value from the above values.
    
                $cookie = "Cookie: JSESSIONID=33eyr1y736486zcnl0vtmo12;XSRF-TOKEN=$XSRF_TOKEN;QCSession=$QCSession;ALM_USER=$ALM_USER;LWSSO_COOKIE_KEY=$LWSSO_COOKIE_KEY";
    
                // echo $cookie;
    
                $curl = curl_init();
                Header('Content-type: application/json');
                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://host:port/qcbin/api/domains/CET_NTD/projects/BILLING_OPERATIONS/defects",
    
                    // CURLOPT_RETURNTRANSFER => true,
    
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HEADER => 0,
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                        "authorization: Basic " . base64_encode($credentials) ,
                        "cache-control: no-cache",
                        "Accept: application/json",
                        $cookie
                    ) ,
                ));
                $response = curl_exec($curl);
                $err = curl_error($curl);
                curl_close($curl);
                if ($err)
                    {
                    echo "cURL Error #:" . $err;
                    }
                  else
                    {
                    echo $response;
                    }
                }
        ?>