webhdfs通过php中的curl上传

时间:2018-03-16 23:08:28

标签: php hadoop curl webhdfs

由curl-to-PHP生成:http://incarnate.github.io/curl-to-php/

function call_curl($headers, $method, $url, $data,$file,$size){
    $handle = curl_init();

    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    // curl_setopt($handle, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); 
    switch($method) {
            case 'GET':
                break;
            case 'POST':
                curl_setopt($handle, CURLOPT_POST, true);
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
            case 'PUT': 
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                curl_setopt($ch, CURLOPT_INFILE, $file);
                curl_setopt($ch, CURLOPT_INFILESIZE, $size);
                break;
            case 'DELETE':
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
           }

    $response = curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);
    return $response;
}

$target_file='test1';
$header = array('Content-Type: application/x-www-form-urlencoded');
$method = "PUT";

$url="http://chbpc-VirtualBox:9864/webhdfs/v1/".$target_file."?op=CREATE&namenoderpcaddress=localhost:9000&createflag=&createparent=true&overwrite=false";

$file = fopen("text.txt", 'r');
$size = filesize("text.txt");
$filedata = fread($file,$size);
$data = array($filedata,$target_file);
$call_2 = call_curl($header, $method, $url, $data,$file,$size);

它只是将标题上传到hdfs而不是文件。我在hdfs中禁用了权限,这就是为什么我没有输入任何用户名和密码。谢谢你的帮助。

0 个答案:

没有答案