PHPackages                             sn01615/request - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. sn01615/request

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sn01615/request
===============

A wrapper for HTTP requests in PHP.

v1.15(5y ago)076WTFPLPHP

Since May 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sn01615/Request)[ Packagist](https://packagist.org/packages/sn01615/request)[ RSS](/packages/sn01615-request/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Request
=======

[](#request)

The 'Request' class is a wrapper for HTTP requests. It provides a collection of simple methods for easily and reliably retrieving information associated with a HTTP request.

Install
-------

[](#install)

```
composer require sn01615/request
```

Usage
-----

[](#usage)

### General Data Access Methods

[](#general-data-access-methods)

The following methods can be used to retrieve data from any of the PHP superglobal arrays associated with a HTTP request: `get`, `post`, `files`, `session`, `cookie`, `env`, and `server`.

For example, to get the 'username' property submitted via a POST request:

```
$username = Request::post('username');

```

The arguments passed to the data access methods are case sensitive. Also, header variables, which are accessible via the `server` method, must be specified in uppercase with underscores rather than hyphens and a with a HTTP\_ prefix.

```
// Request the Cache-Control header:
$cc = Request::server('HTTP_CACHE_CONTROL');

```

The access methods will return NULL if the requested variable is undefined, unless you provide a default value:

```
// Will return Fred Nurk if $_POST['username'] is undefined.
$username = Request::post('username', 'Fred Nurk');

```

If called without a parameter, the methods will return an array containing all variables from the associated superglobal:

```
// Requested: http://a.com/?username=fred&id=1
// Returned:  array('username' => 'fred', 'id' => '1')
$input = Request::get();

```

To simulate HTTP methods not supported by browsers (e.g. PUT, DELETE), assign the method name to a request variable named HTTP\_X\_HTTP\_METHOD\_OVERRIDE. For example:

```
// To simulate the PUT method via a HTML form:

```

To access input data submitted via PUT or DELETE, use the `put` and `delete` methods. They work just like `get` and `post`.

Use the `body` method to get the raw body of a request. An optional default value can be provided.

```
// Requested: The form above is submitted with username 'Fred'
// Returned:  username=Fred&Submit=Submit&HTTP_X_HTTP_METHOD_OVERRIDE=PUT
$body = Request::body();

```

The `input` method also works like the other access methods, and can be used to retrieve input data irrespective of whether it was submitted via a GET, POST, PUT or DELETE request. If called without a parameter, `input` will return an array containing the data from all input superglobals. The `all` method takes it one step further, and will also return data contained within the $\_FILES array.

Use the `only` method to only return a subset of items from the input data:

```
// Only get the email variable from the input data:
$email = Request::only('email');

// Only get the username and email variables from the input data:
$input = Request::only(array('username', 'email'));

```

Conversely, use the `except` method to return all input data except for an item or array of items:

```
// Get all input data except for username and email:
$input = Request::except(array('username', 'email'));

```

The `only` and `except` methods can be called with either a string or an array, but both will always return an array or NULL.

Use the `has` method to check if one or more variables exist within the input data. The method will return FALSE if at least one variable is undefined or empty; otherwise it will return TRUE.

```
// Has `id` been submitted?
if (Request::has('id')) { echo 'The `id` exists.'; }

// Have `id` and `name` both been submitted?
if (Request::has(array('id', 'name'))) { // do cool stuff }

```

### Request Properties

[](#request-properties)

The `host` method returns the domain name, or host name, of the request. A default value can be returned if the host name cannot be determined.

```
// Requested: http://a.com/blog/
// Returned:  a.com
$host = Request::host();

```

The `port` method returns the target port number of the request. If called with a parameter value of TRUE (indicating the result should be "decorated"), the method will prefix the port number with a colon, but will not return a value for standard ports 80 and 443.

```
// Requested: http://a.com/blog/
// Returned:  80
$port = Request::port();

// No value returned for standard ports:
$port = Request::port(TRUE);

```

The `protocol` method returns the version of the HTTP protocol used in the request. The method accepts a default value, but if none is provided, and the protocol cannot be determined, 'HTTP/1.1' will be returned.

The `scheme` method returns the HTTP scheme used in the request (i.e. http or https):

```
// Requested: http://a.com/
// Returned:  http
$scheme = Request::scheme();

// Requested: http://a.com/
// Returned:  http://
$scheme = Request::scheme(TRUE);

```

The `secure` method returns TRUE if the HTTP scheme is 'https' and FALSE if the scheme is 'http':

```
if (Request::secure()) { 'Requested over HTTPS.'; }

```

Use `method` to retrieve the method of the HTTP request (e.g. GET, POST). The result is always returned in uppercase:

```
$method = Request::method();

```

If implementing a REST API, use the `safe` method to determine if the request was "read only" (i.e. GET or HEAD).

Use the `ajax` method to determine if the request was submitted via XMLHttpRequest:

```
if (Request::ajax()) { 'An AJAX request.'; }

```

The `referrer` method returns the address of the request's referrer. A default value can be returned should the referrer be undefined.

The `entrusted` method returns TRUE if all proxy servers are implicitly trusted (i.e. no trusted proxy servers are defined), or the request has passed through a specifically trusted proxy. See below for further detail on defining trusted proxy servers.

### Working with URLs

[](#working-with-urls)

Use the `url` method to return the complete URL of the request and `uri` to return the URL path excluding the query string. The URI will always be returned with a '/' prefix.

```
// Requested: http://a.com/blog/?id=1
// Returned:  http://a.com/blog/?id=1
$url = Request::url();

// Returned: /blog
$uri = Request::uri();

```

The URI is determined by inspection of the following headers: PATH\_INFO, REQUEST\_URI, PHP\_SELF and REDIRECT\_URL. It is possible to extend, or override the use of these headers by specifying custom URI resolvers. This is achieved by calling the `resolvers` method with an array of alternate headers, each with optional modifier functions for tweaking the returned value. The modifier should return FALSE if the URI cannot (or should not) be resolved from the header; any other value returned by the modifier function will be taken as the URI.

```
// Add X_REWRITE_URI as a source for the URI, but only
// if Windows is the server platform:
Request::resolvers(array('X_REWRITE_URI' => function($uri) {
  return stripos(PHP_OS, 'WIN') ? $uri : FALSE;
}));

```

Use the `query` method to return the query string of the request. Call the method with TRUE to include the '?' prefix.

```
// Requested: http://a.com/blog/?name=joe&id=1
// Returned:  name=joe&id=1
$query = Request::query();

```

The `segments` method returns an array containing all URI segments. An optional array can be provided as a default value should the URI be empty.

```
// Requested: http://a.com/blog/admin/posts/
// Returned:  array('blog', 'admin', 'posts');
$segments = Request::segments();

```

An individual URI segment can be retrieved using the `segment` method. The method takes a one-based index and an optional second parameter for the default value. Calling `segment` with a negative index evaluates segments in reverse order.

```
// Requested: http://a.com/blog/admin/posts/
// Returned:  blog
$first = Request::segment(1);

// Returned:  posts
$last = Request::segment(-1);

```

### Client Properties

[](#client-properties)

The `languages` method returns an array containing the languages accepted by a client. The languages are sorted in order of preference (highest preference first).

```
// e.g. array('en-US', 'en');
$languages = Request::languages();

```

The preferred language can be retrieved using the `language` method, which also accepts an optional default value.

The `accepts` method returns an array containing a preferentially ordered list of accepted media types (e.g. 'text/html'), and the `accept` method returns the media type with the highest preference.

By default the `accept` method returns the media type in a "friendly" format (e.g. 'html' or 'json'), but if called with an optional second argument of TRUE, the full media type will be returned.

```
// Requested: http://a.com/code.js
// Returned:  js
// Default:   html
$accepted = Request::accept('html');

// Returned:  application/javascript
$accepted = Request::accept('text/html', FALSE);

```

The Request class can seamlessly translate most of the common media types into a "friendly" format, but you can add support for additional format translations by using the `format` method. The first argument is the friendly format, and the second is the media type:

```
// Support a new media type format:
Request::format('font', array('application/x-font-woff',
  'application/font-off'));

```

The `type` method returns the content type of the body of a request. As with the `accepts` method, two optional arguments are supported; the first sets a default value, and the second controls the format of the result.

The `charsets` method returns an array containing a preferentially ordered list of accepted character sets (e.g. 'utf-8'), and the `charset` method returns the character set with the highest preference.

As with the `language` and `accept` methods, `charset` supports an optional default value.

The `agent` method returns the client user agent as a string (e.g. Mozilla/5.0 (Macintosh; …)).

The `ip` method seeks to return the IP address of the client. The returned value is influenced by factors including whether the value of the HTTP\_CLIENT\_IP header variable is trusted, and whether the request has been made via a trusted proxy server.

By default, both the client, and all proxy servers are trusted. If you do not trust the client, call the method with a value of FALSE:

```
// Do not retrieve the IP address from HTTP_CLIENT_IP:
$ip = Request::ip(FALSE);

```

To only trust an IP address relayed via specific proxy servers, you must first call the `proxies` method, passing an array containing the IP addresses of the trusted proxy servers:

```
// Only trust IP addresses relayed via the proxy 10.0.0.10:
Request::proxies('10.0.0.10');

// To trust 10.0.0.10 and 10.0.0.11:
Request::proxies(array('10.0.0.10', '10.0.0.11'));

```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~565 days

Total

2

Last Release

1999d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6fd6cb8950d77fdebcef7c1cd1eac849e5610deddf19aa539f76b371ccaee152?d=identicon)[sn01615](/maintainers/sn01615)

---

Top Contributors

[![sn01615](https://avatars.githubusercontent.com/u/7794149?v=4)](https://github.com/sn01615 "sn01615 (11 commits)")

---

Tags

request

### Embed Badge

![Health badge](/badges/sn01615-request/health.svg)

```
[![Health](https://phpackages.com/badges/sn01615-request/health.svg)](https://phpackages.com/packages/sn01615-request)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.2k](/packages/guzzlehttp-psr7)[psr/http-message

Common interface for HTTP messages

7.1k1.0B5.5k](/packages/psr-http-message)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[fig/http-message-util

Utility classes and constants for use with PSR-7 (psr/http-message)

39489.0M274](/packages/fig-http-message-util)[nette/http

🌐 Nette Http: abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.

48619.2M541](/packages/nette-http)[psr/http-server-handler

Common interface for HTTP server-side request handler

175101.3M921](/packages/psr-http-server-handler)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
