2012-09-05 3 views
3

저는 REST에 익숙하며 한쪽의 sid (이미 다룹니다)와 다른 쪽의 HP의 ALM에서 JIRA로 연결할 웹 응용 프로그램을 개발하려고합니다.PHP CURL을 사용하는 HP ALM REST API 로그인

내가 지금 시도하고있는 것은 ALM에 대한 기본 인증이지만 진행이되지 않는 것입니다.
여기 내 코드입니다 :

$handle=curl_init('http://192.168.1.7:8081'); 
$headers = array(
    'Accept: application/xml', 
    'Content-Type: application/xml', 
    'Authorization: Basic YWRtaW46MTIzNA==', 
); 

$username='admin'; 
$password='1234'; 

$url = 'http://192.168.1.7:8081/qcbin/authentication-point/login.jsp'; 


curl_setopt_array(
$handle, 
array(
CURLOPT_URL=>'http://192.168.1.7:8081/qcbin/rest/domains/default/projects/Ticomsoft/defects?login-form-required=y', 
//CURLOPT_COOKIEFILE=>$ckfile, 
CURLOPT_POST=>true, 
//CURLOPT_HTTPGET =>true, 
CURLOPT_COOKIEJAR=>$ckfile, 
CURLOPT_VERBOSE=>1, 
//CURLOPT_POSTFIELDS=>, 
//CURLOPT_GETFIELDS=>'j_username=admin&j_password=1234&redirect-url=http://192.168.1.7:8081/myUiResource.jsps', 
CURLOPT_SSL_VERIFYHOST=> 0, 
CURLOPT_SSL_VERIFYPEER=> 0, 
CURLOPT_RETURNTRANSFER=>true, 
CURLOPT_FOLLOWLOCATION=>true, 
CURLOPT_HEADER=>false, 
CURLOPT_HTTPHEADER=> $headers, 
CURLOPT_AUTOREFERER=>true 
//CURLOPT_COOKIE=> 
//CURLOPT_USERPWD=>"admin:yahala" 
//CURLOPT_CUSTOMREQUEST=>"POST" 
) 

); 
$result=curl_exec($handle); 
$ch_error = curl_error($handle); 
$response = curl_getinfo($handle); 

print_r($response); 
if ($ch_error) { 
    echo "cURL Error: $ch_error"; 
} else { 
    //var_dump(json_decode($result, true)); 
    echo $result; 
} 

curl_close($handle); 

?> 

내 시행 착오가 진행으로 많은 쓰레기가 볼 수 있습니다.

답변

7

여기 있습니다. 나는 QC Rests API 문서를 따라 QC가 요청할 것으로 예상되는 순서를 연구했다. ALM11에 대해 테스트했습니다. 나는 새로 입문 한 분이 십니다. 그러나 이것은 당신을 안으로 데려 가서 일해야합니다. ......

<?php 

//create a new cURL resource 
$qc = curl_init(); 
//create a cookie file 
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 

//set URL and other appropriate options 
curl_setopt($qc, CURLOPT_URL, "http://qualityCenter:8080/qcbin/rest/is-authenticated"); 
curl_setopt($qc, CURLOPT_HEADER, 0); 
curl_setopt($qc, CURLOPT_HTTPGET, 1); 
curl_setopt($qc, CURLOPT_RETURNTRANSFER, 1); 

//grab the URL and pass it to the browser 
$result = curl_exec($qc); 
$response = curl_getinfo($qc); 

//401 Not authenticated (as expected) 
//We need to pass the Authorization: Basic headers to authenticate url with the 
//Correct credentials. 
//Store the returned cookfile into $ckfile 
//Then use the cookie when we need it...... 
if($response[http_code] == '401') 
{ 

     $url = "http://qualityCenter:8080/qcbin/authentication-point/authenticate"; 
     $credentials = "qc_username:qc_password"; 
     $headers = array("GET /HTTP/1.1","Authorization: Basic ". base64_encode($credentials)); 

    curl_setopt($qc, CURLOPT_URL, $url); 
    curl_setopt($qc, CURLOPT_HTTPGET,1); //Not sure we need these again as set above? 
    curl_setopt($qc, CURLOPT_HTTPHEADER, $headers); 
    //Set the cookie 
     curl_setopt($qc, CURLOPT_COOKIEJAR, $ckfile); 
     curl_setopt($qc, CURLOPT_RETURNTRANSFER, true); 

     $result = curl_exec($qc); 
     $response = curl_getinfo($qc); 

     //The response will be 200 
     if($response[http_code] == '200') 
     { 
     //Use the cookie for subsequent calls... 
     curl_setopt($qc, CURLOPT_COOKIEFILE, $ckfile); 
     curl_setopt($qc, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($qc, CURLOPT_URL, "http://qualityCenter:8080/qcbin/rest/domains/Your_Domain/projects/Your_Project/defects"); 

    //In this example we are retrieving the xml so... 
     $xml = simplexml_load_string(curl_exec($qc)); 
     print_r($xml); 

    //Call Logout 
     logout($qc,"http://qualityCenter:8080/qcbin/authentication-point/logout"); 

     } 
     else 
     { 
     echo "Authentication failed"; 
     } 

    } 
else 
{ 
     echo "Not sure what happened?!"; 
} 

//Close cURL resource, and free up system resources 
curl_close($qc); 

function logout($qc, $url) 
{ 
    curl_setopt($qc, CURLOPT_URL, $url); 
     curl_setopt($qc, CURLOPT_HEADER, 0); 
     curl_setopt($qc, CURLOPT_HTTPGET,1); 
     curl_setopt($qc, CURLOPT_RETURNTRANSFER, 1); 

    //grab the URL and pass it to the browser 
    $result = curl_exec($qc); 
} 

?> 

작동하면 알려주세요!

감사합니다,

리치

+0

저에게 도움이되지 않습니다. 저를 도와주세요. 여기서 ALM은 설명서를 업그레이드했습니다. [** ALM New Documentation **] (http://alm-help.saas.hp.com/en/12.50/api_refs/REST/webframe.htm#signin-out_example.htm) – Batman

0

다음은이 문제에 대한 펄 내 솔루션입니다 : 인증 단계는 다음 문제없이 수행 할 수있는 다음 libcurl에서 요청에 대한 쿠키를 설정, 먼저 수행된다. 배경 작업을위한 버전입니다. 대화 상자 응용 프로그램의 경우 자격 증명을 대신 사용자 입력에서 전달할 수 있습니다. 또한, http 대신 https를 사용하여이 작업을 수행해야했습니다. Perl 프로그램은 또한 curl에 https를 지시하는 방법을 보여줍니다 (http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/에 아주 좋은 방법이 있습니다). 명심해야 할 중요한 것들 중

#!/usr/bin/perl 

# This script accesses, as a proxy, the REST API of the HP quality center 
# Running it without query parameter, the complete list of defects is returned 
# A query parameter, e.g. 'query={id[2283]}' will be passed as is to the HP QC API 

# We are using the libcurl wrapper WWW::Curl::Easy 
# The access is https, so a certificate has to be passed to libcurl 
# The main point for using curl, however, is the authentication procedure: 
# HP requires a preparative call to a special authentication service 
# The authentication ticket will then be passed back as a cookie 
# Only with this ticket, the real GET request on the defects can be performed 

use WWW::Curl::Easy; 

use strict; 
use warnings; 

use constant { 
    URL_QC_DEFECTS => "https://[QC DOMAIN]/qcbin/rest/domains/[DOMAIN]/projects/[PROJECT]/defects/", 
    URL_QC_AUTH  => "https://[QC DOMAIN]/qcbin/authentication-point/authenticate", 
    PATH_CERT  => "[PATH TO CREDENTIALS]" # contains certificate and credentials, see below 
    }; 

doRequest(URL_QC_DEFECTS . "?" . $ENV{QUERY_STRING}); 
return 0; 

sub doRequest { 
    my ($url,$cookies,$response) = (shift,"",""); 
    eval { 
    my $curl = get_curl_instance(\$cookies,\$response); 
    authenticate($curl); 
    get($curl, $url); 
    if ($response =~ /.*?(<\?xml\b.*)/s) { 
     print "Content-Type:text/xml\n\n"; 
     print $1; 
     } 
    else { 
     die "The response from HP QC is not in XML format"; 
     } 
    }; 
    if ([email protected]) { 
    print "Content-Type:text/plain\n\[email protected]"; 
    } 
    } 

sub get_curl_instance { 

    my ($cookie,$response) = @_; 

    my $curl = WWW::Curl::Easy->new(); 

    open(my $cookiefile, ">", $cookie) or die "$!"; 
    $curl->setopt(CURLOPT_COOKIEFILE, $cookiefile); 
    open(my $responsefile, ">", $response) or die "$!"; 
    $curl->setopt(CURLOPT_WRITEDATA, $responsefile); 
    $curl->setopt(CURLOPT_SSL_VERIFYPEER, 1); 
    $curl->setopt(CURLOPT_SSL_VERIFYHOST, 2); 
    $curl->setopt(CURLOPT_CAINFO, cert()); 
    $curl->setopt(CURLOPT_FOLLOWLOCATION, 1); 
    return $curl; 
    } 

sub authenticate { 
    my $curl = shift; 
    my ($rc,$status); 
    $curl->setopt(CURLOPT_URL, URL_QC_AUTH); 
    $curl->setopt(CURLOPT_USERPWD, cred()); 
    if (($rc = $curl->perform()) != 0) { 
    die "Error Code $rc in curl->perform() on URL " . URL_QC_AUTH; 
    } 
    if (($status=$curl->getinfo(CURLINFO_HTTP_CODE))!="200") { 
    die "HTTP-Statuscode $status from authentication call"; 
    } 
    } 


sub get { 
    my ($curl,$url) = @_; 
    my ($rc,$status); 
    $curl->setopt(CURLOPT_URL, $url); 
    $curl->setopt(CURLOPT_HEADER, { Accept => "text/xml" }); 
    if (($rc = $curl->perform()) != 0) { 
    die "Error Code $rc from defects request"; 
    } 
    if (($status=$curl->getinfo(CURLINFO_HTTP_CODE))!="200") { 
    die "HTTP Statuscode $status from defects request"; 
    } 
    } 

sub cred { 
    open CRED, PATH_CERT . '/.cred_qc' or die "Can't open credentials file: $!"; 
    chomp(my $cred = <CRED>); 
    close CRED; 
    return $cred; 
    } 

sub cert { 
    return PATH_CERT . '/qc.migros.net.crt'; 
    } 
2

하나는이 를 반환합니다 다음 POST/qcbin/휴식/사이트 세션을 쿠키 LWSSO

을 수행해야 인증 이후 QCSession모든 작업을 수행하는 데 필요한 XSRF-TOKEN

관련 문제