2011-02-18 86 views
37

我想用cURL創建一個HTTP PUT請求,我無法讓它工作。我讀過很多教程,但其中沒有一個真正起作用。這是我現在的代碼:PHP cURL HTTP PUT

$filedata = array('metadata' => $rdfxml); 
$ch = curl_init($url); 
$header = "Content-Type: multipart/form-data; boundary='123456f'"; 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata)); 
$returned = curl_exec($ch); 

if (curl_error($ch)) 
{ 
    print curl_error($ch); 
} 
else 
{ 
    print 'ret: ' .$returned; 
} 

我也嘗試使用PHP梨,但得到了相同的結果。問題是存儲庫說沒有設置元數據。我真的需要幫助!謝謝!

回答

74

只是一直在做,今天我自己...這裏是代碼,我的工作對我來說...

$data = array("a" => $a); 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data)); 

$response = curl_exec($ch); 

if (!$response) 
{ 
    return false; 
} 

源:http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

+1

不e,我嘗試使用`curl_setopt($ curl,CURLOPT_PUT,true);`以及這段代碼,它不起作用,所以`curl_setopt($ curl,CURLOPT_PUT,true);`必須被移除。 – 2016-06-24 10:22:31

0

郵差使用Chrome瀏覽器,選擇CODE你這個.. 。和工程

<?php 
 

 
$curl = curl_init(); 
 

 
curl_setopt_array($curl, array(
 
    CURLOPT_URL => "https://blablabla.com/comorl", 
 
    CURLOPT_RETURNTRANSFER => true, 
 
    CURLOPT_ENCODING => "", 
 
    CURLOPT_MAXREDIRS => 10, 
 
    CURLOPT_TIMEOUT => 30, 
 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
 
    CURLOPT_CUSTOMREQUEST => "PUT", 
 
    CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"[email protected]\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}", 
 
    CURLOPT_HTTPHEADER => array(
 
    "cache-control: no-cache", 
 
    "content-type: application/json", 
 
    "x-api-key: whateveriyouneedinyourheader" 
 
), 
 
)); 
 

 
$response = curl_exec($curl); 
 
$err = curl_error($curl); 
 

 
curl_close($curl); 
 

 
if ($err) { 
 
    echo "cURL Error #:" . $err; 
 
} else { 
 
    echo $response; 
 
} 
 

 
?>