Download the PHP package jalsoedesign/no-dependency-http-server without Composer

On this page you can find all versions of the php package jalsoedesign/no-dependency-http-server. 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 no-dependency-http-server

No Dependency HTTP Server

What it says on the box; a very very basic HTTP server with absolutely zero dependencies.

Requirements

This library uses the PHP socket library, so please follow the info here: https://www.php.net/manual/en/sockets.installation.php

Most PHP installations already come with this extension.

Usage

There's 3 main methods:

HttpServer::infinite

HttpServer::infinite($callback, $port = 5000, $host = '127.0.0.1', $options = null) runs indefinitely, until a false boolean is returned.

<?php

use jalsoedesign\NoDependencyHttpServer\HttpServer;
use jalsoedesign\NoDependencyHttpServer\HttpRequest;
use jalsoedesign\NoDependencyHttpServer\HttpResponse;
use jalsoedesign\NoDependencyHttpServer\StatusCode;

require_once(__DIR__ . '/../vendor/autoload.php');

$port = 5000;

printf('Listening on http://localhost:%d/..' . PHP_EOL, $port);

HttpServer::infinite(function(HttpRequest $request) {
    $body = $request->getBody();

    $htmlLines = [
        sprintf('Body: <strong>%s</strong>', htmlentities($body)),
        sprintf('Request method: <strong>%s</strong>', htmlentities($request->getRequestMethod())),
        sprintf('Request URL: <strong>%s</strong>', htmlentities($request->getRequestUrl())),
        sprintf('HTTP version: <strong>%s</strong>', htmlentities($request->getHttpVersion())),
    ];

    $html = implode('<br />' . PHP_EOL, $htmlLines);

    return new HttpResponse(StatusCode::OK, $html, ['Content-Type' => 'text/html']);
}, $port);

If you want to exit after something happened (eg. when you receive an OAuth token), you can simply return false;:

<?php

use jalsoedesign\NoDependencyHttpServer\HttpServer;
use jalsoedesign\NoDependencyHttpServer\HttpRequest;
use jalsoedesign\NoDependencyHttpServer\HttpResponse;
use jalsoedesign\NoDependencyHttpServer\StatusCode;

require_once(__DIR__ . '/../vendor/autoload.php');

$port = 5000;

printf('Listening on http://localhost:%d/..' . PHP_EOL, $port);

$code = null;

HttpServer::infinite(function(HttpRequest $request) use (&$code) {
    $requestUrl = $request->getRequestUrl();
    parse_str(urldecode(parse_url($requestUrl, PHP_URL_QUERY)), $query);

    if ( ! empty($query['code'])) {
        $code = $query['code'];

        $html = sprintf('Code: %s', $code);
    } else {
        $code = null;

        $html = 'No "code" was found in query';
    }

    // Make sure we use sendResponse() directly, so that we can return FALSE to exit the loop
    $request->sendResponse(new HttpResponse(StatusCode::OK, $html, ['Content-Type' => 'text/html']));

    if (!empty($code)) {
        return false; // Exit
    }
}, $port);

printf('Found code: %s', $code);

HttpServer::once

HttpServer::once($callback, $port = 5000, $host = '127.0.0.1', $options = null) runs only for the first request. After the first request is parsed the server will stop.

<?php

use jalsoedesign\NoDependencyHttpServer\HttpServer;
use jalsoedesign\NoDependencyHttpServer\HttpRequest;
use jalsoedesign\NoDependencyHttpServer\HttpResponse;
use jalsoedesign\NoDependencyHttpServer\StatusCode;

require_once(__DIR__ . '/../vendor/autoload.php');

$port = 5000;

printf('Listening on http://localhost:%d/..' . PHP_EOL, $port);

HttpServer::once(function(HttpRequest $request) {
    $body = $request->getBody();

    $htmlLines = [
        sprintf('Body: <strong>%s</strong>', htmlentities($body)),
        sprintf('Request method: <strong>%s</strong>', htmlentities($request->getRequestMethod())),
        sprintf('Request URL: <strong>%s</strong>', htmlentities($request->getRequestUrl())),
        sprintf('HTTP version: <strong>%s</strong>', htmlentities($request->getHttpVersion())),
    ];

    $html = implode('<br />' . PHP_EOL, $htmlLines);

    return new HttpResponse(StatusCode::OK, $html, ['Content-Type' => 'text/html']);
}, $port);

printf('Server ended' . PHP_EOL);

Serve files

If all you want to do is serve a file of folders, simply use HttpServer::serveFiles($serveFolder, $port = 5000, $host = '127.0.0.1', $options = null):

<?php

use jalsoedesign\NoDependencyHttpServer\HttpServer;
use jalsoedesign\NoDependencyHttpServer\HttpRequest;
use jalsoedesign\NoDependencyHttpServer\HttpResponse;
use jalsoedesign\NoDependencyHttpServer\StatusCode;

require_once(__DIR__ . '/../vendor/autoload.php');

$port = 5000;
$fileDirectory = realpath(__DIR__ . '/../serve-files'); // This folder is NOT part of the git project

printf('Listening on http://localhost:%d/, serving files from %s..' . PHP_EOL, $port, $fileDirectory);

HttpServer::serveFiles($fileDirectory, $port);

Options

Options can be supplied as the last argument in HttpServer::infinite and HttpServer::once, and can customize some internal server stuff if needed:

<?php

use jalsoedesign\NoDependencyHttpServer\HttpServer;
use jalsoedesign\NoDependencyHttpServer\HttpRequest;
use jalsoedesign\NoDependencyHttpServer\HttpResponse;
use jalsoedesign\NoDependencyHttpServer\HttpServerOptions;
use jalsoedesign\NoDependencyHttpServer\StatusCode;

require_once(__DIR__ . '/../vendor/autoload.php');

$port = 5000;

printf('Listening on http://localhost:%d/..' . PHP_EOL, $port);

$httpServerOptions = HttpServerOptions::build()
    ->setIncomingClientsBacklog(5)
    ->setUsleepTime(300)
    ->setReadBufferSize(1024);

HttpServer::once(function(HttpRequest $request) {
    return new HttpResponse(StatusCode::OK, 'Hello', ['Content-Type' => 'text/html']);
}, $port, '127.0.0.1', $httpServerOptions);

printf('Server ended' . PHP_EOL);

All versions of no-dependency-http-server with dependencies

PHP Build Version
Package Version
Requires ext-sockets Version *
php Version >=5.4
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 jalsoedesign/no-dependency-http-server contains the following files

Loading the files please wait ....