PHP 使用curl传输文件

1、curl

function putFile($url, $param)
{
    $delimiter = uniqid();
    $imageInfo = pathinfo($param['file']);
    //编译请求头格式和数据流
    $data = '';
    $eol = "\r\n";

    $upload = file_get_contents($param['file']);
    $param['filename'] = $imageInfo['basename'];
    unset($param['file']);
    $data .= "--" . $delimiter . $eol . 'Content-Disposition: form-data; name="uface_package"; filename="' . $param['filename'] . '"' . $eol . 'Content-Type:application/zip'. $eol . $eol;

    $data .= $upload . $eol;
    $data .= "--" . $delimiter . "--" . $eol;
    $post_data = $data;
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type:multipart/form-data; boundary=" . $delimiter, "Content-Length: " . strlen($post_data)]);
    $response = curl_exec($curl);
    curl_close($curl);
    return json_decode($response, true);
}

2、应用逻辑调用

$zip->make(public_path('file').'/users.zip')->add($arrFiles)->close();
$aiotRet = putFile(env('AIOT_HEALTH_URL')."/api/v1/uid/known/append",['file'=>public_path('file').'/users.zip']);```

你可能感兴趣的:(php,java,android)