httpsnippet
![Total Downloads](https://img.shields.io/packagist/dt/isaeken/httpsnippet.svg?style=flat-square)
httpsnippet
is a PHP package that enables you to make HTTP request codes for any language. This can be particularly useful for developers who are looking to perform HTTP requests using various programming languages.
Example
Here?s how you can create a GET request code in PHP:
$request = new \IsaEken\HttpSnippet\Request(
method: 'GET',
uri: 'https://example.com'
);
echo \IsaEken\HttpSnippet\HttpSnippet::make($request, 'php.curl')->toString();
Installation
You can install this package via composer:
composer require isaeken/httpsnippet
Usage
The httpsnippet
package allows you to create HTTP request codes in various programming languages. Below are the steps to use this package:
1. Import the Package
Before you start, make sure that you have installed the package via composer. If not, you can install it by running:
composer require isaeken/httpsnippet
Now, import the httpsnippet package in your PHP script:
use IsaEken\HttpSnippet\HttpSnippet;
use IsaEken\HttpSnippet\Request;
require_once __DIR__ . '/vendor/autoload.php';
2. Create a Request
Next, create a request object. You can do this by passing the request method, URI, headers, and body to the Request
class:
$request = new Request(
method: 'GET',
uri: 'https://example.com',
headers: [
'Accept' => 'application/json',
],
body: [
'foo' => 'bar',
],
version: '1.1',
cookies: [
'foo' => 'bar',
],
);
3. Generate the Request Code
Now, use the make method of the HttpSnippet class to generate the request code for the desired programming language. Pass in the request object and the target programming language as parameters.
$code = HttpSnippet::make($request, 'language.identifier');
For instance, to generate the code in PHP using cURL, you would do:
$code = HttpSnippet::make($request, 'php.guzzle');
4. Output or Use the Code
You can now output or use the generated code as needed. For example, to print the code:
echo $code->toString();
// output:
// <?php
//
// $client = new \GuzzleHttp\Client();
//
// $response = $client->request(
// 'GET',
// 'https://example.com',
// [
// 'body' => '{"foo":"bar"}',
// 'headers' => [
// "Accept" => "application/json",
// "Cookie" => "foo=bar",
// ],
// ],
// );
//
// echo $response->getBody();
Or, to save the code to a file:
file_put_contents('code.txt', $code->toString());
Supported Languages and Identifiers
The following languages and identifiers are supported:
| Language | Identifier | Ready for use? |
| --- |----------------------------|----------------|
| C | libcurl
| ? |
| C# | csharp.httpclient
| ? |
| C# | csharp.restsharp
| ? |
| PHP | php.curl
| ? |
| PHP | php.guzzle
| ? |
| Shell | shell.curl
| ? |
| Shell | shell.wget
| ? |
Testing
Run the tests with:
composer test
Changelog
All notable changes to httpsnippet
will be documented in this file
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email hello@isaeken.com.tr instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.