2016-07-20 2 views
0

연결하는 데 사용할 토큰을 제공하는 API로 작업하고 있습니다. 그것은 이러한 지시 사항을 제공합니다.PHP 컬 요청에서 Auth Digest 헤더 변수 사용

이 토큰은 헤더 변수 Auth Digest의 모든 후속 서버 호출에서 전송됩니다.

이것이 의미하는 것이 확실하지 않습니다. 몇 가지 접근 방법을 시도하고 여러 스택 오버플로 질문을 읽습니다. 여기 내가 시도한 것이있다. 자세한 내용은 코드 주석을 참조하십시오.

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 

// I have tried setting both of these 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 

// In combination of the above separately, I have tried each of these individually 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Auth Digest: ' . $token)); 

curl_setopt($ch, CURLOPT_POST, true); 
$post_data = array('Auth Digest' => $token); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); 

curl_setopt($ch, CURLOPT_USERPWD, $token); 

// Then I execute and close, either giving me a failed response and a response that says token is not valid 
$response = curl_exec($ch); 
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT); 
if (!$response) { 
    echo $action . ' curl request failed'; 
    return false; 
} 
curl_close($ch); 
$response_json = json_decode($response); 
var_dump($response_json); 

다음은 성공적인 문제없이 내 문제에 적용하려는 몇 가지 관련 스택 오버 플로우 관련 질문입니다.

How do I make a request using HTTP basic authentication with PHP curl?

Curl request with digest auth in PHP for download Bitbucket private repository

Client part of the Digest Authentication using PHP POST to Web Service

나는 원시 HTTP 헤더가 가능성이 기대하고 있다는 것을 알 필요가 또는 나는 그들이 가능성이 기대하고있는 헤더를 생성하는 PHP 컬을 사용하는 방법 . 귀하의 경우 그래서

Authorization: Digest _data_here_ 

, 시도 :

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
//... existing options here 
$headers = array(
    'Authorization: Digest ' . $token, 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$response = curl_exec($ch); 

당신이 CURLOPT_HTTPHEADER를 사용하는 경우, 바로 보낼 수있는 추가 헤더를 지정하고 필요로하지 않는다는 것을

+1

일반적으로'$ headers = array ("Authorization : Digest $ token");하지만 그보다 더 많은 부분이있을 수 있습니다. API 또는 정확한 세부 정보가 없으면 우리는 추측 할 수 있습니다. – drew010

+0

@ drew010 그 작품입니다. 대답을 게시 할 수 있다면 받아 들일 것입니다. 'CURLOPT_HTTPHEADER'를 사용하지 않고'curl_setopt'를 사용하여 어떻게 할 수 있는지 보여 주시면 많은 도움이 될 것입니다. 헤더의 모든 라인을 수동으로 설정해야합니다. – Goose

+0

답변을 추가했습니다. 귀하의 의견을 완전히 이해하고 있는지 확신 할 수 없지만 CURLOPT_HTTPHEADER를 사용한다고해서 머리글의 모든 행을 수동으로 설정해야한다는 의미는 아닙니다. 단지 컬이 옵션을 갖지 않거나 더 큰 제어를 원하는 추가 헤더를 전달할 수 있습니다. 여전히 다른 모든 일을 할 것입니다. – drew010

답변

1

소화 인증 헤더는 일반적으로처럼 보인다 거기에 모든 헤더를 추가하십시오.

보내 주시는 다른 헤더에 명시적인 옵션이있는 경우이를 사용하고 하나의 인증 헤더를 CURLOPT_HTTPHEADER으로 전달하십시오.