我正在尝试使用REST API
through Curl
&更新 ALM 中的测试数据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
在第 3 步之前工作正常,跳过第 4 步。
我成功了,直到第 3 步,我得到的响应为LOCKED_BY_ME
. 第 4 步我跳过了,因为我们没有版本控制概念。其中第 5 步,当我绑定发送 xml 数据时出现错误415 (Unsupported Media)
,我相信这是因为 ALM 服务器无法识别我发送的数据。我通过发送“<code>Associative array/XMl/JSON”来尝试它,但我得到的状态码是 415。
这是我的代码。
代码
$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);
}
}
}
我明白415 Error
了,我无法使用PUT
方法发送数据。感谢您的帮助。
谢谢