2015-05-30 20 views
1

我是使用API​​封裝器之外的API的全新軟件。我可以訪問使用使用PHP發送DELETE到API

curl -u username:password https://company.c 
om/api/v1/resources/xxxxxxx 

加載了所有信息的API,但我需要做的就是發送一個DELETE基於文件名的陣列上的網址;例如[ '/js/jquery.js']。參數的名稱是Files。

我已經在代碼中的目錄和文件名變量。

$storageFilename = $directoryname . "/" . $asset->name; 

上面返回數據庫中的/ directoryname/filename。

+1

發送DELETE經由CURL使用PHP請求被描述在 http://stackoverflow.com/questions/13420952/php -curl-delete-request –

回答

0

要發送的HTTP(S)刪除使用捲曲庫在PHP:

$url = 'https://url_for_your_api'; 

//this is the data you will send with the DELETE 
$fields = array(
    'field1' => urlencode('data for field1'), 
    'field2' => urlencode('data for field2'), 
    'field3' => urlencode('data for field3') 
); 

/*ready the data in HTTP request format 
*(like the querystring in an HTTP GET, after the '?') */ 
$fields_string = http_build_query($fields); 

//open connection 
$ch = curl_init(); 

/*if you need to do basic authentication use these lines, 
*otherwise comment them out (like, if your authenticate to your API 
*by sending information in the $fields, above. */ 
$username = 'your_username'; 
$password = 'your_password'; 
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password); 
/*end authentication*/ 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 

/*unless you have installed root CAs you can't verify the remote server's 
*certificate. Disable checking if this is suitable for your application*/ 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

//perform the HTTP DELETE 
$result = curl_exec($ch); 

//close connection 
curl_close($ch); 

/* this answer builds on David Walsh's very good HTTP POST example at: 
* http://davidwalsh.name/curl-post 
* modified here to make it work for HTTPS and DELETE and Authentication */ 
+1

你在哪裏發送登錄信息? – TheDizzle

+0

好點。我編輯了答案,包括登錄使用基本認證的API。 (有些人會將您的身份驗證信息作爲字段傳遞,對於這些類型的API,只需註釋基本身份驗證行。) – Aaron