php 请求方法、请求类型

请求方法、请求类型

// 返回请求类型
function get_request_method() {
    return $_SERVER['REQUEST_METHOD'];
}
// 是否为POST请求
function is_post() {
    if ($_POST) {
        return true;
    } else {
        return false;
    }
}

// 是否为GET请求
function is_get() {
    if ($_GET) {
        return true;
    } else {
        return false;
    }
}

// 是否为PUT请求
function is_put() {
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        return true;
    } else {
        return false;
    }
}

// 是否为PATCH请求
function is_patch() {
    if ($_SERVER['REQUEST_METHOD'] == 'PATCH') {
        return true;
    } else {
        return false;
    }
}

// 是否为DELETE请求
function is_delete() {
    if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
        return true;
    } else {
        return false;
    }
}

// 是否为AJAX请求
function is_ajax() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        return true;
    } else {
        return false;
    }
}

备注:直接复制可用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PHP支持多种HTTP请求方法,包括常见的GET、POST、PUT、DELETE等。下面是各种方法的简要介绍: 1. GET方法:用于从服务器获取资源,通常将参数附加在URL末尾发送请求。 ```php $url = 'http://example.com/api/resource'; $response = file_get_contents($url); ``` 2. POST方法:用于向服务器提交数据,通常用于表单提交等操作。 ```php $url = 'http://example.com/api/resource'; $data = array('key1' => 'value1', 'key2' => 'value2'); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 3. PUT方法:用于向服务器更新资源,通常用于更新已有的数据。 ```php $url = 'http://example.com/api/resource'; $data = array('key1' => 'new_value1', 'key2' => 'new_value2'); $options = array( 'http' => array( 'method' => 'PUT', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 4. DELETE方法:用于从服务器删除资源,通常用于删除已有的数据。 ```php $url = 'http://example.com/api/resource'; $options = array( 'http' => array( 'method' => 'DELETE', ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 注意:以上示例使用`file_get_contents()`函数发送HTTP请求,也可以使用其他HTTP请求库,如cURL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laravel技术社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值