Using HP ALM REST API with PHP and cURL with XML format
Last active
August 29, 2015 14:27
-
-
Save killan/9966ad3e16cf6c3391e7 to your computer and use it in GitHub Desktop.
Using HP ALM REST API with PHP and cURL with XML format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$eol = "\r\n"; | |
$mime_boundary = 'boundary'; | |
$content = '--' . $mime_boundary . $eol; | |
$content .= 'Content-Disposition: form-data; name="filename"'; | |
$content .= $eol . $eol; | |
$content .= $filename; | |
$content .= $eol; | |
/*$content .= '--'.$mime_boundary. $eol; | |
$content .= 'Content-Disposition: form-data; name="description"'; | |
$content .= $eol; | |
$content .= 'Description'; | |
$content .= $eol; | |
$content .= $eol;*/ | |
$content .= '--' . $mime_boundary . $eol; | |
$content .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"'; | |
$content .= $eol; | |
$content .= 'Content-Type: ' . $contentType; | |
$content .= $eol . $eol; | |
$handle = fopen($path . DIRECTORY_SEPARATOR . $filename, 'r'); | |
$content .= fread($handle, filesize($path . DIRECTORY_SEPARATOR . $filename)); | |
fclose($handle); | |
$content .= $eol; | |
$content .= '--' . $mime_boundary . '--'; | |
$header = array( | |
'Content-Type: multipart/form-data; boundary=' . $mime_boundary, | |
'Content-Length: ' . strlen($content), | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $content); | |
curl_setopt($ch, CURLOPT_COOKIE, 'LWSSO_COOKIE_KEY='.$authCookie); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_PROXY, null); //If applicable | |
curl_setopt($ch, CURLOPT_URL, 'http://[server]:[port]/qcbin/rest/domains/' . $domain. '/projects/' . $project . '/defects/' .$id . '/attachments'); | |
$output = curl_exec($ch); | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://[server]:[port]/qcbin/authentication-point/logout'); | |
curl_setopt($ch, CURLOPT_COOKIE, 'LWSSO_COOKIE_KEY='.$authCookie); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_PROXY, null); //If applicable | |
$output = curl_exec($ch); | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$payload = "<alm-authentication><user>".$user."</user><password>".$pw."</password></alm-authentication>"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://[server]:[port]/qcbin/authentication-point/alm-authenticate'); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); | |
curl_setopt($ch, CURLOPT_PROXY, null); //If applicable | |
$output = curl_exec($ch); | |
curl_close($ch); | |
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $output, $matches); | |
$cookies = array(); | |
foreach ($matches[1] as $item) { | |
parse_str($item, $cookie); | |
$cookies = array_merge($cookies, $cookie); | |
} | |
$authCookie = $cookies['LWSSO_COOKIE_KEY']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$date = utf8_encode(date("Y-m-d")); | |
$detectedby = utf8_encode("username"); | |
$owner = utf8_encode("owner"); | |
$title = utf8_encode("summary"); | |
$payload = <<<EOF | |
<Entity Type="defect"> | |
<Fields> | |
<Field Name="detected-by"> | |
<Value>$detectedby</Value> | |
</Field> | |
<Field Name="owner"> | |
<Value>$owner</Value> | |
</Field> | |
<Field Name="creation-time"> | |
<Value>$date</Value> | |
</Field> | |
<Field Name="status"> | |
<Value>Open</Value> | |
</Field> | |
<Field Name="name"> | |
<Value>$title</Value> | |
</Field> | |
</Fields> | |
</Entity> | |
EOF; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://[server]:[port]/qcbin/rest/domains/' . $domain. '/projects/' . $project . '/defects'); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); | |
curl_setopt($ch, CURLOPT_COOKIE, 'LWSSO_COOKIE_KEY='.$authCookie); | |
curl_setopt($ch, CURLOPT_PROXY, null); //If applicable | |
$output = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
libxml_use_internal_errors(true); | |
$xml = simplexml_load_string($output); | |
$id = null; | |
if ($xml !== false) { | |
$node = $xml->xpath('//Field[@Name="id"]/Value'); | |
if (isset($node[0])) { | |
$id = intval((string) $node[0]); | |
} | |
} | |
if (is_numeric($id)) { | |
//Success | |
} else { | |
//Fail | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment