2012-11-16 207 views
71

我想用PHP和cURL做一個DELETE http請求。PHP CURL刪除請求

我已閱讀了如何做到這一點很多地方,但似乎沒有爲我工作。

這是我要做的事:

public function curl_req($path,$json,$req) 
{ 
    $ch = curl_init($this->__url.$path); 
    $data = json_encode($json); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data))); 
    $result = curl_exec($ch); 
    $result = json_decode($result); 
    return $result; 
} 

我再繼續使用我的功能:

public function deleteUser($extid) 
{ 
    $path = "/rest/user/".$extid."/;token=".$this->__token; 
    $result = $this->curl_req($path,"","DELETE"); 
    return $result; 

} 

這給了我HTTP內部服務器錯誤。 在我使用GET和POST使用相同curl_req方法的其他函數中,一切都很順利。

那麼我做錯了什麼?

+3

內部服務器錯誤意味着有一個與腳本收到您的請求時出現問題。 – Brad

+0

感謝布拉德 - 我知道,我猜它是因爲它沒有發送DELETE請求。如果我使用Firefox的REST客戶端插件並使用DELETE發送完全相同的請求,則它可以正常工作。因此它像cURL一樣不會將請求發送爲DELETE。 – Bolli

+0

相關? http://stackoverflow.com/questions/2081894/handling-put-delete-arguments-in-php –

回答

139

我終於自己解決了這個問題。如果其他人是否有這個問題,這裏是我的解決方案:

我創建了一個新的方法:

public function curl_del($path) 
{ 
    $url = $this->__url.$path; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    $result = curl_exec($ch); 
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

    return $result; 
} 

更新2

因爲這似乎幫助一些人,這是我最後的curl DELETE方法,它返回JSON解碼對象中的HTTP響應:

/** 
* @desc Do a DELETE request with cURL 
* 
* @param string $path path that goes after the URL fx. "/user/login" 
* @param array $json If you need to send some json with your request. 
*       For me delete requests are always blank 
* @return Obj $result HTTP response from REST interface in JSON decoded. 
*/ 
public function curl_del($path, $json = '') 
{ 
    $url = $this->__url.$path; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch); 
    $result = json_decode($result); 
    curl_close($ch); 

    return $result; 
} 
+0

你能告訴我我們如何做ajax調用的PHP(方法:刪除)持有這個刪除捲曲代碼並從ajax傳遞它的值? – user1788736

+0

@ user1788736我不擅長Ajax,但我想你可以創建一個執行此方法的PHP文件,並使用Ajax將您的數據使用POST發送到該PHP文件。如果您認爲上述方法令人困惑,請再看一遍。 $ url s就是您需要與之通話的服務器(http://someserver.com),$ path是URL(/ something /)之後的內容。我將這些分開的唯一原因是因爲我需要始終發送到同一臺服務器,但是需要動態路徑。希望這是有道理的。 – Bolli

+0

不需要標題? –

-13
$json empty 

public function deleteUser($extid) 
{ 
    $path = "/rest/user/".$extid."/;token=".$this->__token; 
    $result = $this->curl_req($path,"**$json**","DELETE"); 
    return $result; 

} 
+0

謝謝。在這個特定的REST調用中,JSON部分需要是空的,所以這不成問題。但是,無論如何感謝 – Bolli

+0

這裏'$ json empty'是什麼意思?它不在這個函數的範圍內,所以'$ json'的用法不會做任何事情。 – halfer

+0

我已要求刪除此答案,但主持人表示不支持。無論如何,這個答案的海報自2014年起還沒有登錄。 – halfer

0

我自己的帶有wsse認證的類請求

class Request { 

    protected $_url; 
    protected $_username; 
    protected $_apiKey; 

    public function __construct($url, $username, $apiUserKey) { 
     $this->_url = $url;  
     $this->_username = $username; 
     $this->_apiKey = $apiUserKey; 
    } 

    public function getHeader() { 
     $nonce = uniqid(); 
     $created = date('c'); 
     $digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->_apiKey, true)); 

     $wsseHeader = "Authorization: WSSE profile=\"UsernameToken\"\n"; 
     $wsseHeader .= sprintf(
      'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->_username, $digest, $nonce, $created 
     ); 

     return $wsseHeader; 
    } 

    public function curl_req($path, $verb=NULL, $data=array()) {      

     $wsseHeader[] = "Accept: application/vnd.api+json"; 
     $wsseHeader[] = $this->getHeader(); 

     $options = array(
      CURLOPT_URL => $this->_url . $path, 
      CURLOPT_HTTPHEADER => $wsseHeader, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_HEADER => false    
     );     

     if(!empty($data)) { 
      $options += array(
       CURLOPT_POSTFIELDS => $data, 
       CURLOPT_SAFE_UPLOAD => true 
      );       
     } 

     if(isset($verb)) { 
      $options += array(CURLOPT_CUSTOMREQUEST => $verb);       
     } 

     $ch = curl_init(); 
     curl_setopt_array($ch, $options); 
     $result = curl_exec($ch);     

     if(false === $result) { 
      echo curl_error($ch); 
     } 
     curl_close($ch); 

     return $result; 
    } 
} 
+0

使用+ = instaead array_merge –

+0

,它現在可以工作 –

+0

,但對於這個問題是一個不必要的複雜解決方案。 –

6

要調用GET,POST,DELETE,把所有樣的要求,我已創建了一個共同的功能

function CallAPI($method, $api, $data) { 
    $url = "http://localhost:82/slimdemo/RESTAPI/" . $api; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

    switch ($method) { 
     case "GET": 
      curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); 
      break; 
     case "POST": 
      curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 
      break; 
     case "PUT": 
      curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); 
      break; 
     case "DELETE": 
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 
      break; 
    } 
    $response = curl_exec($curl); 
    $data = json_decode($response); 

    /* Check for 404 (file not found). */ 
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    // Check the HTTP Status code 
    switch ($httpCode) { 
     case 200: 
      $error_status = "200: Success"; 
      return ($data); 
      break; 
     case 404: 
      $error_status = "404: API Not found"; 
      break; 
     case 500: 
      $error_status = "500: servers replied with an error."; 
      break; 
     case 502: 
      $error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!"; 
      break; 
     case 503: 
      $error_status = "503: service unavailable. Hopefully they'll be OK soon!"; 
      break; 
     default: 
      $error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl); 
      break; 
    } 
    curl_close($curl); 
    echo $error_status; 
    die; 
} 

CALL刪除方法

$data = array('id'=>$_GET['did']); 
$result = CallAPI('DELETE', "DeleteCategory", $data); 

CALL Post方法

$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']); 
$result = CallAPI('POST', "InsertCategory", $data); 

調用get方法

$data = array('id'=>$_GET['eid']); 
$result = CallAPI('GET', "GetCategoryById", $data); 

CALL put方法

$data = array('id'=>$_REQUEST['eid'],m'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']); 
$result = CallAPI('POST', "UpdateCategory", $data);