Requests
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Top Related Projects
Guzzle, an extensible PHP HTTP client
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
PHP's lightweight HTTP client
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Quick Overview
WordPress/Requests is a PHP HTTP library designed to be a simpler alternative to cURL. It provides a user-friendly API for making HTTP requests, handling cookies, and managing authentication. The library is designed to work seamlessly on various PHP environments and supports both procedural and object-oriented coding styles.
Pros
- Simple and intuitive API for making HTTP requests
- Supports multiple authentication methods (Basic, Digest, NTLM)
- Automatic cookie handling and session management
- PSR-7 compliant, allowing easy integration with other PHP libraries
Cons
- Limited advanced features compared to more comprehensive HTTP clients
- May not be suitable for complex scenarios requiring fine-grained control
- Dependency on PHP's built-in stream functions, which can be disabled in some environments
Code Examples
- Making a simple GET request:
$response = Requests::get('https://api.example.com/users');
echo $response->body;
- Sending a POST request with data:
$data = ['username' => 'johndoe', 'email' => 'john@example.com'];
$response = Requests::post('https://api.example.com/users', [], $data);
echo $response->status_code;
- Using basic authentication:
$options = ['auth' => ['username', 'password']];
$response = Requests::get('https://api.example.com/protected', [], $options);
echo $response->body;
Getting Started
To use WordPress/Requests in your PHP project, follow these steps:
-
Install the library using Composer:
composer require rmccue/requests
-
Include the Composer autoloader in your PHP script:
require 'vendor/autoload.php';
-
Make HTTP requests using the Requests class:
$response = Requests::get('https://api.example.com/data'); if ($response->success) { $data = json_decode($response->body); // Process the data } else { echo "Error: " . $response->status_code; }
With these steps, you can start making HTTP requests using the WordPress/Requests library in your PHP applications.
Competitor Comparisons
Guzzle, an extensible PHP HTTP client
Pros of Guzzle
- More feature-rich with advanced functionality like concurrent requests and streaming
- Better performance for large-scale applications
- Extensive middleware system for customizing request/response handling
Cons of Guzzle
- Steeper learning curve due to more complex API
- Heavier footprint, may be overkill for simple projects
- Requires PHP 7.2+ (Requests supports older PHP versions)
Code Comparison
Requests:
$response = Requests::get('https://api.example.com/data');
$data = json_decode($response->body);
Guzzle:
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'https://api.example.com/data');
$data = json_decode($response->getBody());
Summary
Guzzle is a more powerful and feature-rich HTTP client library, suitable for complex applications with advanced requirements. It offers better performance and extensibility but comes with a steeper learning curve.
Requests is simpler and easier to use, making it a good choice for smaller projects or when working with older PHP versions. It provides a straightforward API for basic HTTP operations but lacks some of the advanced features found in Guzzle.
Both libraries offer similar basic functionality, but Guzzle provides more options for customization and handling complex scenarios. The choice between them depends on the specific needs of your project and your familiarity with each library's API.
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
Pros of http-client
- More comprehensive and feature-rich HTTP client with support for advanced features like async requests and HTTP/2
- Better integration with the Symfony ecosystem and other components
- Actively maintained with regular updates and improvements
Cons of http-client
- Steeper learning curve due to more complex API and advanced features
- Heavier dependency footprint, potentially increasing project size
- May be overkill for simple HTTP requests in smaller projects
Code Comparison
Requests:
$response = Requests::get('https://api.example.com/data');
$data = json_decode($response->body);
http-client:
$client = HttpClient::create();
$response = $client->request('GET', 'https://api.example.com/data');
$data = $response->toArray();
Summary
http-client offers more advanced features and better integration with Symfony, making it suitable for complex applications. However, Requests provides a simpler API for basic HTTP operations, which may be preferable for smaller projects or those not using the Symfony framework. The choice between the two depends on the specific requirements of your project and your familiarity with the respective ecosystems.
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
Pros of php-curl-class
- Focused specifically on cURL, providing more advanced cURL-specific features
- Lightweight and easy to integrate into existing projects
- Supports both procedural and object-oriented programming styles
Cons of php-curl-class
- Limited to cURL functionality, less versatile for different HTTP client implementations
- May require more manual configuration for complex requests
- Less extensive documentation compared to Requests
Code Comparison
php-curl-class:
$curl = new Curl();
$curl->get('https://api.example.com');
$response = $curl->response;
Requests:
$response = Requests::get('https://api.example.com');
$body = $response->body;
Summary
php-curl-class is a specialized library for cURL operations, offering more granular control over cURL-specific features. It's lightweight and easy to integrate but limited to cURL functionality. Requests, on the other hand, provides a more versatile HTTP client with support for multiple backends and extensive documentation. The choice between the two depends on the specific needs of the project, with php-curl-class being ideal for cURL-focused applications and Requests offering broader HTTP client capabilities.
PHP's lightweight HTTP client
Pros of Buzz
- More extensive feature set, including async requests and PSR-7 support
- Better suited for complex HTTP operations and modern PHP applications
- Active development with regular updates and improvements
Cons of Buzz
- Steeper learning curve due to more advanced features
- Heavier dependency footprint compared to Requests
- May be overkill for simple HTTP requests in smaller projects
Code Comparison
Requests:
$response = Requests::get('https://api.example.com/data');
$data = json_decode($response->body);
Buzz:
$browser = new Browser();
$response = $browser->get('https://api.example.com/data');
$data = json_decode($response->getBody()->__toString());
Summary
Requests is a lightweight, easy-to-use HTTP library suitable for basic HTTP operations in WordPress and other PHP projects. It has a simple API and minimal dependencies.
Buzz offers more advanced features and better integration with modern PHP practices, making it ideal for complex applications. However, it may be more complex to set up and use for simpler tasks.
Choose Requests for straightforward HTTP requests in WordPress or smaller projects, and Buzz for more sophisticated HTTP operations in larger, modern PHP applications.
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Pros of Httpful
- More intuitive and fluent API design for chaining methods
- Built-in support for various authentication methods (Basic, Digest, NTLM)
- Better handling of file uploads and multipart requests
Cons of Httpful
- Less actively maintained compared to Requests
- Smaller community and fewer contributors
- Limited support for advanced features like parallel requests
Code Comparison
Httpful:
$response = \Httpful\Request::get($url)
->expectsJson()
->send();
echo $response->body->name;
Requests:
$response = Requests::get($url);
$data = json_decode($response->body);
echo $data->name;
Summary
Httpful offers a more intuitive API and built-in support for various authentication methods, making it easier to work with complex requests. However, Requests has a larger community, more active maintenance, and better support for advanced features. Httpful's fluent interface allows for more readable code, but Requests provides a simpler approach that may be preferred by some developers. The choice between the two libraries depends on specific project requirements and personal preferences.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Pros of Requests
- Actively maintained with regular updates and bug fixes
- Extensive documentation and community support
- Wider range of features and functionality
Cons of Requests
- Larger codebase, potentially more complex for simple use cases
- May have a steeper learning curve for beginners
Code Comparison
Requests:
$response = Requests::get('https://api.example.com/data');
if ($response->success) {
$data = json_decode($response->body);
// Process $data
}
Requests>:
$response = Requests::get('https://api.example.com/data');
if ($response->status_code === 200) {
$data = json_decode($response->body);
// Process $data
}
The code comparison shows that both libraries have similar syntax for making HTTP requests. The main difference is in how they handle response status checks. Requests uses a success
property, while Requests> relies on checking the status_code
directly.
Both libraries provide a straightforward way to make HTTP requests in PHP, but Requests offers more features and ongoing development. Requests> may be suitable for simpler projects or those requiring a lightweight solution. Consider your project's specific needs when choosing between the two.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Requests for PHP
Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python library. Requests is ISC Licensed (similar to the new BSD license) and has no dependencies, except for PHP 5.6.20+.
Despite PHP's use as a language for the web, its tools for sending HTTP requests are severely lacking. cURL has an interesting API, to say the least, and you can't always rely on it being available. Sockets provide only low level access, and require you to build most of the HTTP response parsing yourself.
We all have better things to do. That's why Requests was born.
$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = WpOrg\Requests\Requests::get('https://api.github.com/gists', $headers, $options);
var_dump($request->status_code);
// int(200)
var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"
var_dump($request->body);
// string(26891) "[...]"
Requests allows you to send HEAD, GET, POST, PUT, DELETE, and PATCH HTTP requests. You can add headers, form data, multipart files, and parameters with basic arrays, and access the response data in the same way. Requests uses cURL and fsockopen, depending on what your system has available, but abstracts all the nasty stuff out of your way, providing a consistent API.
Features
- International Domains and URLs
- Browser-style SSL Verification
- Basic/Digest Authentication
- Automatic Decompression
- Connection Timeouts
Installation
Install with Composer
If you're using Composer to manage dependencies, you can add Requests with it.
composer require rmccue/requests
or
{
"require": {
"rmccue/requests": "^2.0"
}
}
Install source from GitHub
To install the source code:
$ git clone git://github.com/WordPress/Requests.git
Next, include the autoloader in your scripts:
require_once '/path/to/Requests/src/Autoload.php';
You'll probably also want to register the autoloader:
WpOrg\Requests\Autoload::register();
Install source from zip/tarball
Alternatively, you can fetch a tarball or zipball:
$ curl -L https://github.com/WordPress/Requests/tarball/stable | tar xzv
(or)
$ wget https://github.com/WordPress/Requests/tarball/stable -O - | tar xzv
Using a Class Loader
If you're using a class loader (e.g., Symfony Class Loader) for PSR-4-style class loading:
$loader = new Psr4ClassLoader();
$loader->addPrefix('WpOrg\\Requests\\', 'path/to/vendor/Requests/src');
$loader->register();
Documentation
The best place to start is our prose-based documentation, which will guide you through using Requests.
After that, take a look at the documentation for
\WpOrg\Requests\Requests::request()
, where all the parameters are fully
documented.
Requests is 100% documented with PHPDoc. If you find any problems with it, create a new issue!
Test Coverage
Requests strives to have 100% code-coverage of the library with an extensive set of tests. We're not quite there yet, but we're getting close.
Requests and PSR-7/PSR-18
PSR-7 describes common interfaces for representing HTTP messages. PSR-18 describes a common interface for sending HTTP requests and receiving HTTP responses.
Both PSR-7 as well as PSR-18 were created after Requests' conception. At this time, there is no intention to add a native PSR-7/PSR-18 implementation to the Requests library.
However, the amazing Artur Weigandt has created a package, which allows you to use Requests as a PSR-7 compatible PSR-18 HTTP Client. If you are interested in a PSR-7/PSR-18 compatible version of Requests, we highly recommend you check out this package.
Contribute
Contributions to this library are very welcome. Please read the Contributing guidelines to get started.
Top Related Projects
Guzzle, an extensible PHP HTTP client
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
PHP's lightweight HTTP client
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot