I want to POST using HTTP_Request2 Pear Class. I was succefull when I used cURL to do the same, but I dont get response data when I use HTTP_Request. It says content length as 0. I read the PEAR documentation for HTTP_Request2 and followed it to write the code. It will be of great help if someone points out my errors. cURL method works but HTTP_Request2 method dosent. What I think is that the HTTP_Request2 method is unable to post the data, but I am not sure about the header too. My code is
我想使用http_request2pear类发布帖子。当我使用cURL来做同样的事情时,我是成功的,但是当我使用HTTP_Request时,我没有得到响应数据。它表示内容长度为0。我阅读了关于HTTP_Request2的PEAR文档,并遵循它来编写代码。如果有人指出我的错误,将会有很大帮助。cURL方法有效,但HTTP_Request2方法dosent。我认为HTTP_Request2方法无法发布数据,但我也不确定标题是否正确。我的代码是
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 个解决方案
#1
0
The result of the HTTP_Request2::send()
method is a little different to curl_exec
. It is not as string, but another type, namely HTTP_Request2_Response
.
HTTP_Request2::send()方法的结果与curl_exec略有不同。它不是字符串,而是另一种类型,即HTTP_Request2_Response。
To retrieve the response body as a string (a HTTP response contains the headers and a body), use the HTTP_Request2_Response::getBody
method:
要将响应主体作为字符串检索(HTTP响应包含header和正文),请使用HTTP_Request2_Response::getBody方法:
...
$response = $request->send();
$responseBody = $response->getBody();
This should do what you're looking for, $responseBody
then is a string. In more general terms: HTTP_Request2
has an object-oriented interface. This allows to use different adapters (e.g. Curl as well as sockets or you can even write your own one, e.g. for testing) as well as retrieving the response body in a streaming fashion (e.g. with large responses you do not put all into a single string at once).
这应该做你想要的,$responseBody是一个字符串。更一般的术语:HTTP_Request2有一个面向对象的接口。这允许使用不同的适配器(例如,Curl和套接字,或者您甚至可以编写自己的适配器,例如用于测试),以及以流方式检索响应主体(例如,您不会一次性将所有的响应都放入一个字符串中)。
#1
0
The result of the HTTP_Request2::send()
method is a little different to curl_exec
. It is not as string, but another type, namely HTTP_Request2_Response
.
HTTP_Request2::send()方法的结果与curl_exec略有不同。它不是字符串,而是另一种类型,即HTTP_Request2_Response。
To retrieve the response body as a string (a HTTP response contains the headers and a body), use the HTTP_Request2_Response::getBody
method:
要将响应主体作为字符串检索(HTTP响应包含header和正文),请使用HTTP_Request2_Response::getBody方法:
...
$response = $request->send();
$responseBody = $response->getBody();
This should do what you're looking for, $responseBody
then is a string. In more general terms: HTTP_Request2
has an object-oriented interface. This allows to use different adapters (e.g. Curl as well as sockets or you can even write your own one, e.g. for testing) as well as retrieving the response body in a streaming fashion (e.g. with large responses you do not put all into a single string at once).
这应该做你想要的,$responseBody是一个字符串。更一般的术语:HTTP_Request2有一个面向对象的接口。这允许使用不同的适配器(例如,Curl和套接字,或者您甚至可以编写自己的适配器,例如用于测试),以及以流方式检索响应主体(例如,您不会一次性将所有的响应都放入一个字符串中)。