[已解决]我在ubuntu14上有一个hbase 1.1.2独立安装,我需要通过rest服务器通过php post请求检索和更新数据(由于长度限制,我不能使用get)。我本来打算使用一个curl php对象,但我的问题是我不知道如何格式化请求(json或xml)以在post-at-rest服务器中提交。
有人能帮我吗?
谢谢
让我添加我想用于所有请求的对象:按键获取行和set\create row。我的问题是如何根据不同的操作生成$数据字段。
function method($method, $data=NULL, & $http_code, & $response)
{
$ch = curl_init();
if(isset($header))
{
curl_setop(CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_URL, "http://" . $GLOBALS['DBDW']['hostname'] . $GLOBALS['DBDW']['port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//nuovi
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Connection: ' . ( $this->options['alive'] ? 'Keep-Alive' : 'Close' ),
));
// fine nuovi
switch(strtoupper($method)){
case 'DELETE':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
// i have to set CURLOPT_POSTFIELDS and if yes how?
break;
case 'POST':
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
// how setting CURLOPT_POSTFIELDS and if yes how?
break;
case 'GET':
curl_setopt($curl, CURLOPT_HTTPGET, 1);
// i have to set CURLOPT_POSTFIELDS and if yes how?
break;
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->log(LOG_DEBUG, "HTTP code: " . $http_code, __FILE__, __LINE__, __CLASS__);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($response === false)
{
$this->log(LOG_ERR, "HTTP " . $method . " failed on url '" . $url . "'", __FILE__, __LINE__, __CLASS__);
$this->log(LOG_ERR, "cURL error: " . $curl_errno . ":" . $curl_error, __FILE__, __LINE__, __CLASS__);
$http_code=ERR_COMMUNICATION_FAILED;
return false;
}
return true;
}
1条答案
按热度按时间2j4z5cfb1#
CURLOPT_POSTFIELDS
只有在使用post时才需要,因此您的switch/case是正确的。如果您想发出get请求,只需将get内容附加到url,就像您在浏览器中看到的那样。此功能非常方便:
http_build_query
.这是我用于 curl 的函数。如果你愿意,你可以参考它(或使用它)。