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

前端 未结 2 419
故里飘歌
故里飘歌 2020-12-05 03:38

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_ini         


        
2条回答
  •  执念已碎
    2020-12-05 04:11

    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/

提交回复
热议问题