2012-09-16 3 views
2

HTTP_Request2 배 클래스를 사용하여 POST를 원합니다. 내가 cURL을 사용하여 동일한 작업을 수행했을 때 succefull 이었지만 HTTP_Request를 사용하면 응답 데이터가 표시되지 않습니다. 내용 길이를 0으로 지정했습니다. HTTP_Request2에 대한 PEAR 설명서를 읽고 코드를 작성했습니다. 누군가 내 실수를 지적하면 큰 도움이 될 것입니다. cURL 메소드는 작동하지만 HTTP_Request2 메소드는 작동합니다. 내가 생각하기에 HTTP_Request2 메소드는 데이터를 게시 할 수 없지만 헤더에 대해서는 확실하지 않다. 내 코드PHP : HTTP_Request2는 콘텐츠 길이가 0입니다.

function header() 
{ 
$this->setGuid(guid()); 
$this->header = array($this->service, 
time(), $this->getGuid()); 
return $this->header; 
} 

function header1() 
{ 
$this->setGuid(guid()); 
$this->header = array('X-OpenSRF-service: '.$this->service, 
'X-OpenSRF-xid: '.time(), 'X-OpenSRF-thread: '.$this->getGuid()); 
return $this->header; 
} 
function toArray() 
{ 
$url4 = urldata($this->method, $this->param); 
return $url4; //returns an encoded url 
} 
function send1() 
{ 
require_once 'HTTP/Request2.php'; 

//------cURL Method------------------------- 
$endpoint = $this->endpoint; 
$data = $this->toArray(); 
$header = $this->header1(); 
$url_post = 'http://'.$endpoint.'/osrf-http-translator'; 
$this->curl = curl_init(); 
curl_setopt($this->curl, CURLOPT_URL, $url_post); 
curl_setopt($this->curl, CURLOPT_HEADER, 1); 
curl_setopt($this->curl, CURLOPT_POST, 1); 
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); 
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); 
$this->server_result = curl_exec($this->curl); 
if (curl_error($this->curl) != 0) { 
$error = 'Curl error: ' . curl_error($this->curl); 
return $error; 
} 
var_dump ($this->server_result); 
echo "<HR />"; 

//-----HTTP_REQUEST2 Method---------------  
$request = new HTTP_Request2(); 
$request->setUrl($url_post); 
$request->setHeader(array('X-OpenSRF-service' => $header[0], 'X-OpenSRF-xid' => $header[1], 'X-OpenSRF-thread' => $header[2])); 
$request->setMethod(HTTP_Request2::METHOD_POST); 
$request->addPostParameter($data); 
var_dump ($request); echo "<HR />"; 
$response = $request->send(); var_dump($response); 
} 
+1

코드를 제대로 들여 쓰기 때문에 읽는 것이 더 쉬워 지므로 코드에 약간의 문제가 있으므로 질문에 대답하는 데 도움이 될 것입니다. – hakre

+0

죄송합니다, 내 코드 http://pastebin.com/7pU9UG7r – Pranjal

답변

0

HTTP_Request2::send() 방법의 결과는 curl_exec에 약간의 차이가 있습니다. 문자열이 아니고 다른 유형 인 HTTP_Request2_Response입니다.

사용, (A HTTP 응답 헤더와 본문을 포함) 문자열로 HTTP_Request2_Response::getBody 방법 응답 본문을 검색하려면 :

... 
$response = $request->send(); 
$responseBody = $response->getBody(); 

이것은 당신이 찾고있는 무엇을해야 하는가를 $responseBody 다음이다 끈. 좀 더 일반적인 용어로는 HTTP_Request2에 객체 지향 인터페이스가 있습니다. 이것은 다른 어댑터 (예 : Curl과 소켓을 사용하거나 테스트 용으로 직접 작성할 수도 있습니다)와 스트리밍 방식으로 응답 본문을 검색하는 것을 허용합니다 (예 : 큰 응답으로 모두를 단일 한 번에 문자열).

+0

... $ response = $ request-> send(); $ responseBody = $ response-> getBody(); var_dump ($ responseBody); 은 다음과 같이 출력을 제공합니다. string (0) "" – Pranjal

+0

사실 내가 직면 한 문제는 콘텐츠 길이가 0입니다 !! – Pranjal

+1

더 많은 문제 해결이 필요합니다. var_dump ($ response-> getStatus(), $ response-> getReasonPhrase(), $ response-> getHeader()); 또한 모든 응답 헤더의 var_dump를 제공하십시오. – hakre

관련 문제