PHP Classes

PHP HTTP Code Sample Snippet Generator in Multiple Languages: Generate code snippets to make HTTP requests

Recommend this page to a friend!
  Info   View files Example   View files View files (62)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-09-13 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 28 This week: 28All time: 10,906 This week: 5Up
Version License PHP version Categories
httpsnippet 1.0.0MIT/X Consortium ...7HTTP, Code Generation, PHP 7
Description 

Author

This package can generate code snippets to make HTTP requests.

It can compose an HTTP request from parameters to send that request, for instance, the request URL, HTTP method, request headers, parameters in the request body, HTTP version, cookie values, etc...

The package can generate code in different languages using different libraries to send the configured HTTP request.

Currently, the package can generate code to send the request using:

- PHP code with the Curl extension

- C code using the libcurl library

- C# code using the HTTP Client class

- C# using RestSharp HTTP client library

- PHP using the Guzzle package

- Shell script using the curl command

- Shell script using the wget command

Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Innovation award
Innovation award
Nominee: 13x

Details

httpsnippet

Latest Version on Packagist GitHub Tests Action Status Total Downloads

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.

  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagebin (1 file)
Files folder imageconfig (1 file)
Files folder imagesrc (4 files, 6 directories)
Files folder imagetests (3 files, 1 directory)
Plain text file .editorconfig Data Auxiliary data
Plain text file .php_cs.dist.php Example Example script
Plain text file CHANGELOG.md Data Auxiliary data
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file phpunit.xml Data Auxiliary data
Plain text file README.md Doc. Read me

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (3 files)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Plain text file php-cs-fixer.yml Data Auxiliary data
  Plain text file run-tests.yml Data Auxiliary data
  Plain text file update-changelog.yml Data Auxiliary data

  Files folder image Files  /  bin  
File Role Description
  Plain text file httpsnippet Example Example script

  Files folder image Files  /  config  
File Role Description
  Plain text file httpsnippet.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
Files folder imageAbstracts (1 file)
Files folder imageContracts (1 file)
Files folder imageEnums (1 file)
Files folder imageExceptions (1 file)
Files folder imageLanguages (4 directories)
Files folder imageTraits (3 files, 1 directory)
  Plain text file CodeGenerator.php Class Class source
  Plain text file helpers.php Aux. Helper script
  Plain text file HttpSnippet.php Class Class source
  Plain text file Request.php Class Class source

  Files folder image Files  /  src  /  Abstracts  
File Role Description
  Plain text file AbstractLanguage.php Class Class source

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file Language.php Class Class source

  Files folder image Files  /  src  /  Enums  
File Role Description
  Plain text file ContentType.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file LanguageNotFoundException.php Class Class source

  Files folder image Files  /  src  /  Languages  
File Role Description
Files folder imageC (1 file)
Files folder imageCSharp (2 files)
Files folder imagePhp (2 files)
Files folder imageShell (2 files)

  Files folder image Files  /  src  /  Languages  /  C  
File Role Description
  Plain text file LibCurl.php Class Class source

  Files folder image Files  /  src  /  Languages  /  CSharp  
File Role Description
  Plain text file HttpClient.php Class Class source
  Plain text file RestSharp.php Class Class source

  Files folder image Files  /  src  /  Languages  /  Php  
File Role Description
  Plain text file Curl.php Class Class source
  Plain text file Guzzle.php Class Class source

  Files folder image Files  /  src  /  Languages  /  Shell  
File Role Description
  Plain text file Curl.php Class Class source
  Plain text file Wget.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
Files folder imageRequest (7 files)
  Plain text file HasHttpSnippet.php Class Class source
  Plain text file HasLanguage.php Class Class source
  Plain text file HasRequest.php Class Class source

  Files folder image Files  /  src  /  Traits  /  Request  
File Role Description
  Plain text file HasBody.php Class Class source
  Plain text file HasCookies.php Class Class source
  Plain text file HasHeaders.php Class Class source
  Plain text file HasMethod.php Class Class source
  Plain text file HasProtocolVersion.php Class Class source
  Plain text file HasRequestTarget.php Class Class source
  Plain text file HasUri.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagesnippets (2 directories)
  Plain text file CodeGeneratorTest.php Class Class source
  Plain text file RequestTest.php Class Class source
  Plain text file UnitTest.php Class Class source

  Files folder image Files  /  tests  /  snippets  
File Role Description
Files folder imagebasic (6 files)
Files folder imagephp (1 directory)

  Files folder image Files  /  tests  /  snippets  /  basic  
File Role Description
  Plain text file c.libcurl.txt Doc. Documentation
  Plain text file csharp.httpclient.txt Doc. Documentation
  Plain text file csharp.restsharp.txt Doc. Documentation
  Plain text file php.curl.txt Doc. Documentation
  Plain text file shell.curl.txt Doc. Documentation
  Plain text file shell.wget.txt Doc. Documentation

  Files folder image Files  /  tests  /  snippets  /  php  
File Role Description
Files folder imageguzzle (16 files)

  Files folder image Files  /  tests  /  snippets  /  php  /  guzzle  
File Role Description
  Plain text file application-form-encoded.php Example Example script
  Plain text file application-json.php Example Example script
  Plain text file cookies.php Example Example script
  Plain text file custom-method.php Example Example script
  Plain text file full.php Example Example script
  Plain text file headers.php Example Example script
  Plain text file https.php Example Example script
  Plain text file jsonObj-multiline.php Example Example script
  Plain text file jsonObj-null-value.php Example Example script
  Plain text file multipart-data.php Example Example script
  Plain text file multipart-file.php Example Example script
  Plain text file multipart-form-data-no-params.php Example Example script
  Plain text file multipart-form-data.php Example Example script
  Plain text file query.php Example Example script
  Plain text file short.php Example Example script
  Plain text file text-plain.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:28
All time:10,906
This week:5Up