简体   繁体   中英

HP ALM REST API send xml data using PUT method using Curl & PHP

I am trying to update the tests data in ALM using REST API through Curl & PHP .

Step 1 : Check Lock Status => GET method | response => UNLOCKED

Step 2 : Acquire Lock => POST method | response <entity data XML>

Step 3 : Check Lock Status => GET method | response LOCKED_BY_ME

Step 4 : Checkout Versions | Optional

Step 5 : Update data => PUT method | response = http_code = 200 OK

Working fine until step 3, skip step 4.

I succeeded until step 3 , I get the response as LOCKED_BY_ME . Step 4 I skipped because Versioning concept we don't have. Wherein Step 5, when I am tying the send the xml data I get error 415 (Unsupported Media) , I believe which is because the ALM server is not able to identify the data I am sending. I tried it by sending as “ Associative array/XMl/JSON ” but for all I got status code as 415.

Here is my Code.

Code

$url="http://{Host:port}/qcbin/rest/domains/{domainname}/projects/{Projectname}/tests/{id}/lock";

function update_tests()
{

    $qc=$this->curl_auth();
    $headers = array("DELETE /HTTP/1.1","Content-Type:text/xml");
curl_setopt($qc, CURLOPT_TIMEOUT, $timeout);
curl_setopt($qc, CURLOPT_URL, $url);
    curl_setopt($qc, CURLOPT_RETURNTRANSFER, true);
curl_setopt($qc, CURLOPT_COOKIESESSION, true);
curl_setopt($qc, CURLOPT_COOKIEJAR,BASEPATH .'cookie.txt');
$res_unlock=curl_exec($qc);
    $resp=curl_getinfo($qc);

    $xml=simplexml_load_string(curl_exec($qc));

    if($xml->LockStatus == 'LOCKED_BY_OTHER' || $xml->LockStatus == 'LOCKED_BY_ME')
{
    $headers = array(
        'Accept: application/xml',
        'Content-Type: application/xml',
    );

    $data= <<<XML
<Entity Type="test">
<Fields>
<Field Name="Name">
<Value>Name of the test</Value> 
</Field>
<Field Name="Status">
<Value>Updated Value</Value>
</Field>
</Fields>
</Entity>
XML;

        curl_setopt($qc, CURLOPT_URL, $url);
        curl_setopt($qc, CURLOPT_HTTPHEADER, $header);
    curl_setopt($qc, CURLOPT_HEADER, 1);
    curl_setopt($qc, CURLOPT_PUT, 1);
    curl_setopt($qc, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($qc, CURLOPT_POSTFIELDS, $data);
    curl_setopt($qc, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($qc);
    $resp=curl_getinfo($qc);
}
else
{
        $data='';
        curl_setopt($qc, CURLOPT_URL, $url);

    curl_setopt($qc, CURLOPT_HEADER, true);
    curl_setopt($qc, CURLOPT_POST, 1);
    curl_setopt($qc, CURLOPT_POSTFIELDS, $data);
    $res_Lock=curl_exec($qc);
        $resp=curl_getinfo($qc);

        if($resp['http_code'] == 200)
        {
        $headers = array(
           'Accept: application/xml',
           'Content-Type: application/xml',
        );


        $file = BASEPATH."tests.xml";
        $data= <<<XML
<Entity Type="test">
<Fields>
<Field Name="Name">
<Value>Name of the test</Value> 
</Field>
<Field Name="Status">
<Value>Updated Value</Value> 
</Field>
</Fields>
</Entity>
XML;

           curl_setopt($qc, CURLOPT_URL, $url);
               curl_setopt($qc, CURLOPT_HTTPHEADER, $header);
           curl_setopt($qc, CURLOPT_HEADER, 1);
           curl_setopt($qc, CURLOPT_PUT, 1);
           curl_setopt($qc, CURLOPT_CUSTOMREQUEST, "PUT");
           curl_setopt($qc, CURLOPT_POSTFIELDS, $data);
           curl_setopt($qc, CURLOPT_RETURNTRANSFER, 1);
           $result = curl_exec($qc);
           $resp=curl_getinfo($qc);
        }
    }
}

I get 415 Error , I am not able to send the data using PUT method. Appreciate you kind help.

Thanks

In the above, it looks like your HTTP header is in the variable $headers but you're setting the curl options for HTTP header from $header.

I've been struggling today with a similar problem with HP ALM. Everything started working correctly when I was 100% sure that the following headers were being set:

Content-Type: application/xml Accept: application/xml

There appear to be three things to get right for the HP ALM interface to work:

  1. The URL - obviously
  2. The http headers - nearly isn't good enough
  3. The payload - which looks right from the above

The documentation https://almelsevier.saas.hp.com/qcbin/Help/doc_library/api_refs/REST/webframe.html is just about passable.

You should be using CURLOPT_INFILE with a PUT action. POSTFIELDS is for a POST action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM