如何在PHP中启动GET/POST/PUT/DELETE请求并判断请求类型?

use*_*729 15 php request

我从来没有看到如何PUT/DELETE发送请求.

如何用PHP做到这一点?

我知道如何使用curl发送GET/POST请求:

$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);
Run Code Online (Sandbox Code Playgroud)

但是如何做PUT/ DELETE要求?

Gor*_*don 47

对于DELETE使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
对于PUT使用curl_setopt($ch, CURLOPT_PUT, true);

不依赖于安装卷曲另一种方法是使用file_get_contents一个自定义的HTTP流上下文.

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

查看这两篇关于使用PHP进行REST的文章