How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

php, request

Solution

For `DELETE` use `curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');` For `PUT` use `curl_setopt($ch, CURLOPT_PUT, true);`

An alternative that doesn't rely on cURL being installed would be to use `file_get_contents` with a custom HTTP stream context.

$result = file_get_contents(
    'http://example.com/submit.php', 
    false, 
    stream_context_create(array(
        'http' => array(
            'method' => 'DELETE' 
        )
    ))
);

Check out these two articles on doing REST with PHP

- http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

- http://www.gen-x-design.com/archives/making-restful-requests-in-php/

Problem

I never see how is `PUT/DELETE` request sent. How to do it in PHP? I know how to send a GET/POST request with curl: ``` $ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 4); ``` But how to do `PUT`/`DELETE` request?

Original source