0
votes

[Solved] I have an HBase 1.1.2 standalone installation on Ubuntu 14 and I need to retrieve and update data by PHP POST request (I cannot use GET because of length limit) through a REST server. I was going to use a curl PHP object but my problem is I don't understand how to format the request (in JSON or XML) to be submitted in POST at REST server.

Can anyone help me?

Thanks

Let me add my object i would like to use for all the request: getting rows by key and set\create row. My problem is how make $DATA field according different action.

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
What have you tried so far? share some code. Or are you expecting code from scratch? - taxicala
yeah, is the data already represented in your php code? you have some vars you want transferred to the JSON representation? - Shawn Mehan
Where is this alleged $DATA var? - I wrestled a bear once.
$data is the var I want to fill with the Json request. I don't know what request for each case. - omambe
$DATA is not the same as $data - I wrestled a bear once.

1 Answers

0
votes

CURLOPT_POSTFIELDS is only required if you're using post, so you switch/case is correct as is.

If you want to make GET requests, you just append the GET stuff to the url, just like you would see it in your broswer. This function is handy for that purpose: 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);

This is the function I use for cURL stuff. You can refer to it (or use it) if you want.