[SOLVED] PVE Rest API - ISO Image Upload mit PHP cURL mit HTTP Error 502.

hitman

Renowned Member
Jan 5, 2018
111
30
68
36
Hallo zusammen,

ich versuche per PHP mit cURL Post eine ISO Image an die PVE API "/api2/json/nodes/pve1/storage/cephfs/upload" hochzuladen. Leider erhalte ich immer Error 502.

Wenn jemand einen Tip hat vielen Danke :)

PHP Curl
PHP:
# API URL
$url_api = "https://$dc_host:$dc_port/api2/json/nodes/pve1/storage/cephfs/upload";

# File to Upload
$file = "/home/pbs3.iso";

# Curl Header
$headers = array(
        "CSRFPreventionToken: $dc_token",
        'Content-type: multipart/form-data'
        );

# Curl Post Fields
$postFields = [
   'filename' => curl_file_create($file, mime_content_type($file)),
   'content' => 'iso',
];

# http_build_query
$postFieldString = http_build_query($postFields);

$ch = curl_init($url_api);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldString);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=$dc_ticket");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$ch_output = curl_exec($ch);

Ausgabe der Curl mit HTTP Error 502
Code:
(
    [url] => https://192.168.20.11:8006/api2/json/nodes/pve1/storage/cephfs/upload
    [content_type] => text/html
    [http_code] => 502
    [header_size] => 138
    [request_size] => 551
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 5.021247
    [namelookup_time] => 0.002089
    [connect_time] => 0.002211"
    [pretransfer_time] => 0.004153
    [size_upload] => 93
    [size_download] => 11939
    [speed_download] => 2377
    [speed_upload] => 18
    [download_content_length] => 11939
    [upload_content_length] => 93
    [starttransfer_time] => 0.004157
    [redirect_time] => 0
    [redirect_url] =>
    [primary_ip] => 192.168.20.11
    [certinfo] => Array
        (
        )

    [primary_port] => 8006
    [local_ip] => 192.168.20.11
    [local_port] => 42346
    [http_version] => 3
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 4010
    [connect_time_us] => 2211
    [namelookup_time_us] => 2089
    [pretransfer_time_us] => 4153
    [redirect_time_us] => 0
    [starttransfer_time_us] => 4157
    [total_time_us] => 5021247
)
 
Hallo zusammen, habe die Lösung gefunden.

PHP:
  # POST FILE
  $sitz = $_FILES[ 'images' ][ 'size' ];
  $type = $_FILES[ 'images' ][ 'type' ];
  $name = $_FILES[ 'images' ][ 'name' ];
  $temp = $_FILES[ 'images' ][ 'tmp_name' ];
  $temp = urlencode( $temp );   
 
  # Final Folder
  $file = "/upload/iso/$name";
  # Copy Tempfile to Final Folder
  copy( $_FILES[ 'images' ][ 'tmp_name' ], $file );

# Check    of existis
if (function_exists('curl_file_create')) {
  $tmp_file = curl_file_create($file);
} else { //
  $tmp_file = '@' . realpath($file);
};
# Array for Curl Post
$data = array('content' => 'iso', 'filename' => $tmp_file);

# URL to Rest API for Upload
$url = "https://192.168.20.11:8006/api2/json/nodes/pve1/storage/cephfs/upload";   
    
$headers = array();
$headers = array(
        "CSRFPreventionToken: $dc_token"
        );
    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=$dc_ticket");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_exec ($ch);
curl_close ($ch);
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!