webhdfs在php中通过curl上传

mec1mxoz  于 2021-06-01  发布在  Hadoop
关注(0)|答案(0)|浏览(234)

由curl到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中禁用了权限,所以我没有输入任何用户名和密码。谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题