district5/php-postman-detect

Postman detector 是一个 PHP 库,用于检测请求是否由 Postman 发起。

0.0.1 2023-07-05 20:01 UTC

This package is auto-updated.

Last update: 2024-09-06 11:10:47 UTC


README

当使用 Postman 进行模拟请求时,防止或限制对某些 API 端点的访问。

用法...

"repositories":[
    {
        "type": "vcs",
        "url": "git@github.com:district-5/php-postman-detect.git"
    }
],

"require": {
    "district5/php-postman-detect": ">=0.0.1"
}

代码使用...

<?php

$_SERVER['HTTP_USER_AGENT'] = 'PostmanRuntime/7.26.8';
\District5\PostmanDetect\PostmanDetector::isPostman(); // true

$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko)';
\District5\PostmanDetect\PostmanDetector::isPostman(); // false


$_SERVER['HTTP_USER_AGENT'] = 'PostmanRuntime/7.26.8';

// Disallow this endpoint with Postman always
\District5\PostmanDetect\PostmanDetector::disallowAlways(); // throws exception

// Current environment is 'local'
// disallow on production
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('local', ['prod']); // no exception
// disallow on production and staging
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('local', ['prod', 'staging']); // no exception

// Current environment is 'prod'
// disallow on production
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('prod', ['prod']); // throws exception
// disallow on production and staging
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('prod', ['prod', 'staging']); // throws exception

测试...

composer install
./vendor/bin/phpunit