通过CURL使用PHP向HBase REST发送POST JSon请求


Make POST JSon requests to HBase REST by CURL with PHP

[Solved]我在Ubuntu 14上安装了一个HBase 1.1.2独立安装,我需要通过REST服务器通过PHP POST请求(由于长度限制,我不能使用GET)检索和更新数据。我打算使用curl PHP对象,但我的问题是我不明白如何格式化请求(JSON或XML)在REST服务器上提交POST。

有人能帮我吗?

感谢

让我添加我的对象,我想使用所有的请求:获取行键和设置'创建行。我的问题是如何使$DATA字段根据不同的动作。

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;
}

CURLOPT_POSTFIELDS只有在你使用post时才需要,所以你切换/大小写是正确的。

如果你想要GET请求,你只要把GET的东西附加到url,就像你在浏览器中看到的那样。这个函数很方便:http_build_query

// define the url in a variable above the switch
$url = "http://" . $GLOBALS['DBDW']['hostname'] . $GLOBALS['DBDW']['port'];
switch(strtoupper($method)){
    case 'DELETE':
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');  
        // append the GET parameters if any
        if(!empty($data)) $url .= "?".http_build_query($data);
        break;
    case 'POST':
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case 'GET':
        curl_setopt($curl, CURLOPT_HTTPGET, 1);
        // append the GET parameters if any
        if(!empty($data)) $url .= "?".http_build_query($data);
        break;
}
// move this line below the switch
curl_setopt($ch, CURLOPT_URL, $url);

这是我用于cURL的函数。您可以参考(或使用)它,如果你想。