Download the PHP package devuri/http without Composer

On this page you can find all versions of the php package devuri/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package http

HttpClient

The HttpClient is a PHP component designed to facilitate HTTP requests within PHP applications. It provides a simple, flexible utility for making HTTP requests. It's a basic straightforward way to interact with RESTful APIs from PHP applications.

Features

Requirements

Installation

To install the HttpClient, you need to have Composer installed on your machine. Run the following command in your project directory:

Usage

Basic Usage

Setting Custom User Agent

Setting the Referrer

Configuration

The HttpClient constructor accepts an array of options for configuration:

Example of setting custom configuration:

Examples

Below are some concrete examples to demonstrate the usage of the HttpClient class for various common tasks such as fetching data from an API, sending data, setting custom headers, and handling the response.

Fetching Data from an API

To fetch data using a GET request:

This example retrieves a list of posts from the placeholder API and prints the result.

Sending Data with a POST Request

To send data to an API using a POST request:

This code sends data to create a new post on the placeholder API and outputs the API's response.

Setting Custom Headers

To set custom headers for a request:

This example shows how to include custom headers in a GET request, which is useful for APIs that require specific headers.

Handling Responses and Errors

This example demonstrates how to handle responses and errors gracefully in your application. It checks the status code and throws an exception if the request was not successful, allowing for robust error handling.

Combining Parameters and Options

You can also combine various parameters and options like setting referrers, user agents, and API keys together:

This configuration shows a more complex setup, where multiple options are configured to tailor the HTTP client behavior according to specific needs.

Error Handling

Exceptions are caught internally, but it is recommended to handle potential errors or exceptions in your application logic, especially for critical operations.

HttpClient handles connection errors by returning a status code and error message. Here's how you might handle errors:

Handling Responses

The HttpClient uses PHP's file_get_contents function for making HTTP requests and provides detailed response handling, including HTTP status codes and error messages. Response from requests will typically include status code, message, and any headers returned by the server.

Response Array Structure

When using the HttpClient class to make HTTP requests, the response returned by methods such as get or post will be an associative array containing various pieces of information related to the HTTP response. Here's an explanation of what might be included in this response array, followed by a detailed example:

Common Components of the Response Array

Example of a Response Array

Here is an example to illustrate how the response array might look after a GET request to an API endpoint:

Assuming the request was successful, the output might look something like this:

Detailed Breakdown:

Handling and Utilizing the Response

You can use this structured response to check the status of the HTTP request, log information, parse headers for session or caching purposes, and use the body's content (not shown in this array but typically handled separately as a string or JSON object). This structured approach allows for robust error handling and integration in more complex application logic, ensuring that your application can react appropriately to different responses from external services.

Security: Handling API Responses

When interacting with external APIs using the HttpClient or any similar tool, it's crucial to be aware of the security implications involved in handling the data and responses you receive. Here are some important considerations and best practices to ensure the security of your applications:

Validate API Responses

Always assume that any data received from external sources could be potentially malicious. Validate and sanitize all incoming data rigorously before using it in your application. This includes:

Handle Sensitive Data Carefully

If the API transmits sensitive data, ensure that it is transmitted securely using TLS (HTTPS). Avoid logging sensitive data such as API keys, user credentials, or personal information, as logs can be a vector for data leaks.

Secure Authentication Tokens

If your application uses authentication tokens (e.g., API keys, OAuth tokens):

Manage Error Handling

Proper error handling can prevent unwanted information disclosure:

Set Timeout and Size Limits

Configure timeouts and limit the size of the response to avoid denial-of-service attacks that attempt to tie up resources:

Be Cautious with Redirections

Automatically following redirects in HTTP requests can lead to security vulnerabilities:

Regularly Update Dependencies

Keep all dependencies, including your HTTP client libraries, up to date. Regular updates can protect your applications from known vulnerabilities that could be exploited by attackers.

While the HttpClient provides a convenient way to make HTTP requests, it is essential to be vigilant and implement robust security practices to protect your application and its data. Always approach external data with caution, and ensure that your application is prepared to handle unexpected or malicious data safely and securely.

Scope and Limitations

The HttpClient is designed as a simple and straightforward tool for making HTTP requests in PHP applications. It is suitable for small to medium-sized projects where basic HTTP functionality is required without the overhead of more complex libraries.

When to Use HttpClient

Limitations

While HttpClient is efficient for straightforward tasks, it lacks some of the advanced features and optimizations found in more comprehensive HTTP client libraries. Some of these limitations include:

Considerations for More Complex Needs

For more feature-rich and robust HTTP client capabilities, consider using a more comprehensive library such as GuzzleHTTP. Guzzle provides a vast array of features suitable for complex applications, including:

HttpClient serves as a practical solution for a lightweight and uncomplicated way to make HTTP requests. However, for applications requiring more extensive functionality or handling high volumes of traffic, a more advanced solution like GuzzleHTTP is recommended. Always evaluate the specific needs and complexity of your project to choose the most appropriate tool for the job.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions about using HttpClient, please open an issue.


All versions of http with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
devuri/dot-access Version ^0.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package devuri/http contains the following files

Loading the files please wait ....