PHP CURL DELETE request

后端 未结 5 1976
陌清茗
陌清茗 2020-12-02 12:06

I\'m trying to do a DELETE http request using PHP and cURL.

I have read how to do it many places, but nothing seems to work for me.

This is how I do it:

5条回答
  •  离开以前
    2020-12-02 12:19

    To call GET,POST,DELETE,PUT All kind of request, i have created one common function

    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 Delete Method

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

    CALL Post Method

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

    CALL Get Method

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

    CALL Put Method

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

提交回复
热议问题