2010-10-18 11 views
24

sono in grado di eseguire il seguente comando ricciolo (nella riga di comando) con successo:Usando il metodo PUT con PHP cUrl Biblioteca

curl -XPOST --basic -u user:password -H accept:application/json -H Content-type:application/json --data-binary '{ "@queryid" : 1234 }' http://localhost/rest/run?10 

Ecco quello che sto facendo finora tuttavia non sembra di lavorare con il servizio REST che sto utilizzando:

$headers = array(
    'Accept: application/json', 
    'Content-Type: application/json', 
); 

$url = 'http://localhost/rest/run?10'; 
$query = '{ "@queryid" : 1234 }'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, "user:password"); 

curl_setopt($ch, CURLOPT_PUT, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); 
curl_setopt($ch, CURLOPT_POSTFIELDSIZE, strlen($query)); 

$output = curl_exec($ch); 

echo $output; 

Qual è il modo corretto quando si cerca di convertire --data binario utilizzando un metodo PUT?

risposta

41

Invece di creare un file temporaneo sul disco è possibile utilizzare php://temp.

$body = 'the RAW data string I want to send'; 

/** use a max of 256KB of RAM before going to disk */ 
$fp = fopen('php://temp/maxmemory:256000', 'w'); 

if (!$fp) 
{ 
    die('could not open temp memory data'); 
} 

fwrite($fp, $body); 
fseek($fp, 0); 

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));        

Il lato positivo non è un disco IO, quindi dovrebbe essere più veloce e meno carico sul server.

+1

Questo è geniale! non avevo idea di FD temp, mi ha aiutato a caricare un upload con curl per youtube (leggi i byte rimanenti in mem e quindi caricare normalmente usando il tuo metodo) –

+0

La modalità non dovrebbe essere 'w +'? – flm

34

Ciao a tutti ho ottenuto che funziona con questa configurazione:

// Start curl 
$ch = curl_init(); 
// URL for curl 
$url = "http://localhost/"; 

// Clean up string 
$putString = stripslashes($query); 
// Put string into a temporary file 
$putData = tmpfile(); 
// Write the string to the temporary file 
fwrite($putData, $putString); 
// Move back to the beginning of the file 
fseek($putData, 0); 

// Headers 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Binary transfer i.e. --data-BINARY 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
// Using a PUT method i.e. -XPUT 
curl_setopt($ch, CURLOPT_PUT, true); 
// Instead of POST fields use these settings 
curl_setopt($ch, CURLOPT_INFILE, $putData); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString)); 

$output = curl_exec($ch); 
echo $output; 

// Close the file 
fclose($putData); 
// Stop curl 
curl_close($ch); 

:)

+2

+1 Grazie per aver trovato il tempo di tornare e rispondere alla tua Q. Mi hai risparmiato un sacco di tempo – Basic

5

Tutto ciò che deve essere impostato è la richiesta personalizzata per riutilizzare il metodo post.

CURLOPT_URL=>$url, 
CURLOPT_CUSTOMREQUEST=>'PUT', 
CURLOPT_POSTFIELDS=>$params, 
+0

questo puntatore mi è stato utile quando si accede a un'API REST in cui nella richiesta non sono stati trasferiti dati effettivi (l'url contiene i parametri) – Loopo

+0

Questo metodo rende molto più semplice la conversazione con PUT. Nessun file di testo, basta usare l'infrastruttura POST. – kratenko

+0

Mi hai salvato ... svalutato ... –