2011-12-13 5 views
0

현재 개발자를위한 API를 제공하기 위해 서버 측에서 oAuth를 구현하려고합니다. 나는 아주 쉬운 문제를 겪고있다. request.php라는 스크립트로 보내진 HTTP 헤더를 처리 할 수 ​​있기를 원합니다.컬링으로 전송 된 HTTP 헤더 처리

어떻게 할 수 있는지 잘 모르겠습니다. 나는 클라이언트에 대한 래퍼를 코딩하고, curl로 request.php에서 http 호출을 시도한다.

$data = array('name' => 'Foo'); 

    $header = array('Content-type: text/plain', 'Content-length: 100'); 
    $ch = curl_init("test"); 

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/request.php'); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 

    $res = curl_exec($ch); 

    $headers = curl_getinfo($ch); 

    curl_close($ch); 

그래서 $ headers에서 http 응답 헤더를 받았지만 request.php에서받은 헤더를 처리하고 싶습니다.

+0

이 조금 더 우리에게 알려주십시오 : 예를 들어

최종 사용자가 API 액세스를 승인하려고합니까? 어떤 oAuth 프로토콜 버전 (1.0a 또는 2.0)을 구현할 예정입니까? – middlehut

답변

1

이가 (HTTP 표준에 정의 된 총 4 문자)를 헤더와 데이터 모두 2 CRLF로 구분해야 할 코드 $res의 원인이됩니다

curl_setopt($s, CURLOPT_HEADER, true);

를 사용해야합니다.

HTTP 응답 예,

HTTP/1.0 302 Found 
Content-Type: text/html; charset=UTF-8 
Content-Length: 11782 
Date: Tue, 13 Dec 2011 15:07:19 GMT 
Server: GFE/2.0 

<!DOCTYPE html> 
<html> 
(...) 
</html> 
0

사용 curl_getinfo 헤더를 읽을 수 있습니다. 이렇게하려면 (CURLOPT_HEADER, true)를 설정할 필요가 없습니다. - 2 다리 3 다리가, 예를 들어,

... 
    curl_setopt($this->ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 

    $response = curl_exec($this->ch); 
    $httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);