如何在 PHP 中启动 GET/POST/PUT/DELETE 请求并判断请求类型? [英] How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

查看:22
本文介绍了如何在 PHP 中启动 GET/POST/PUT/DELETE 请求并判断请求类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I never see how is PUT/DELETE request sent.

如何在 PHP 中实现?

How to do it in PHP?

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

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);

但是如何做PUT/DELETE请求?

推荐答案

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

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

不依赖于安装 cURL 的替代方法是使用 file_get_contents自定义 HTTP 流上下文.

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' 
        )
    ))
);

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

Check out these two articles on doing REST with PHP

这篇关于如何在 PHP 中启动 GET/POST/PUT/DELETE 请求并判断请求类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆