geerlingguy/request

A simple PHP HTTP request class.

1.x-dev 2021-12-15 19:06 UTC

This package is auto-updated.

Last update: 2024-10-16 01:26:09 UTC


README

Request for PHP Logo

Request

A simple PHP HTTP request class.

This class includes many convenience methods to help take the headache out of dealing with HTTP requests in PHP.

Usage

Include the class (\JJG\Request) using an autoloader, then build a new Request object, execute the request, and get the response.

$request = new Request('http://www.example.com/');
$request->execute();
$response = $request->getResponse();

Other parameters you can retrieve after executing a request include:

// The full headers from the response.
$request->getHeader();
// The latency for this response, in ms.
$request->getLatency();
// The HTTP status code (e.g. 200 for 200 OK).
$request->getHttpCode();
// Empty if no error present, otherwise shows any cURL errors.
$request->getError();

There are also other convenient methods included for other purposes.

// Returns TRUE if 'string' exists in the response.
$request->checkResponseForContent('string');

You can also make requests with basic HTTP authentication:

// Execute a request with HTTP basic authentication.
$request = new Request('http://www.example.com/secure-page');
$request->setBasicAuthCredentials('username', 'password');
$request->execute();

Other options include enabling or disabling SSL, using cookies, and setting cURL timeout values:

// Enable Cookies.
$request->enableCookies($cookie_file_path);
// Enable SSL/TLS.
$request->enableSSL();
// Set the user agent string.
$request->userAgent = 'User agent string here.';
// Set the initial connection timeout (default is 10 seconds).
$request->connectTimeout = 5;
// Set the timeout (default is 15 seconds).
$request->timeout = 10;
// Send some fields as a POST request.
$request->setRequestType('POST');
$request->setPostFields($field_array);

See the Request class variable definitions and methods for more details and documentation.

Why Request?

I've used other HTTP request libraries for PHP before, but often fall back to using cURL directly, because the libraries I've used are too complicated for my needs. This library aims to be a very simple and easy-to-use wrapper around cURL, and should be easy to pick up for anyone familiar with cURL usage in PHP.

Some other recommended HTTP libraries for PHP include:

License

Request is licensed under the MIT (Expat) license. See included LICENSE.md.