2010-10-18 10 views
24

:kullanma PUT yöntemi PHP cURL Kütüphanesi başarıyla (komut satırında) aşağıdaki bukle komutu çalıştırmak mümkün değilim

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

Ben şimdiye kadar görünmüyor ancak ne yapıyorum

$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; 

bir PUT yöntemi kullanılarak --bilgi-ikili dönüştürmeye çalışırken doğru yolu nedir: Ben kullanıyorum DİNLENME hizmetiyle çalışıyor gibi?

cevap

41

yerine diskte bir geçici dosya oluşturma sen php://temp kullanabilirsiniz.

$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));        

Sunucunun arkasında hiçbir disk yoktur, bu nedenle sunucunuzda daha hızlı ve daha az yüklü olması gerekir.

+1

çok kaydedildi! temp FD'ler hakkında hiçbir fikrim yoktu, youtube için curl ile bir yüklemeye devam etmeme yardımcı oldu (kalan baytları mem'e oku ve sonra yönteminizi kullanarak normal olarak yükle) –

+0

Mod "w +" olmalı mı? – flm

34

Merhaba ben bu yapılandırmayı kullanarak çalışan var hepsi:

// 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 Teşekkür Bu parlak bana zaman – Basic

5

Tüm bu ayarların yapılması, posta yöntemini yeniden kullanmak için yapılan özel istektir. Geri gelip kendi S. yanıtlamak için zaman ayırdığınız için

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

Bu işaretçi, istekte gerçek verilerin aktarılmadığı bir REST API'sine erişirken bana yardımcı oldu (url, parametreleri içeriyor) – Loopo

+0

Bu yöntem, konuşmayı PUT üzerinden çok daha kolay hale getiriyor. Dosyada herhangi bir sorun yok, sadece POST altyapısını kullanın. – kratenko

+0

Beni kurtardınız ... upvoted ... –