2013-04-12 6 views
6

Oppure PHP non lo consente? Ho letto che è possibile utilizzare PUT ma il server si aspetta solo il POST.È possibile utilizzare cURL per caricare in streaming un file utilizzando il POST?

+0

I credo che [pecl_http] (http://pecl.php.net/package/pecl_http) sia in grado di fare qualcosa in quella misura nel ramo 2.x. –

+1

Duplicato di http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php – bwoebi

+0

Il server deve accettare questo con il tipo di contenuto application/octet-stream. – rosc0

risposta

1

Sì è possibile caricamento in streaming un file che utilizza caricamenti di file POST con cURL. Questa è l'impostazione predefinita e devi fornire il nome file dei file che desideri trasmettere in forma di stringhe.

// URL on which we have to post data 
$url = "http://localhost/tutorials/post_action.php"; 
// Any other field you might want to catch 
$post_data['name'] = "khan"; 
// File you want to upload/post 
$post_data['file'] = "@c:/logs.log"; 

// Initialize cURL 
$ch = curl_init(); 
// Set URL on which you want to post the Form and/or data 
curl_setopt($ch, CURLOPT_URL, $url); 
// Data+Files to be posted 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
// Pass TRUE or 1 if you want to wait for and catch the response against the request made 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// For Debug mode; shows up any error encountered during the operation 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
// Execute the request 
$response = curl_exec($ch); 

// Just for debug: to see response 
echo $response; 

Oltre a questo metodo di default, è non possibile utilizzare cURL per lo streaming di caricare un file utilizzando il metodo POST.

5

Curl ha già sostenere il flusso, provare curl --help | grep binary e si otterrà:

"Dati DATI --data-binary HTTP POST binari (H)"

Un esempio: curl -v -XPOST http://example:port/path --data-binary @file.tar \ -H "Content-Type: application/octet-stream"

Problemi correlati