royers/phgo

A simple php library for managing http requests based on the net/http package from golang standard library

Maintainers

Details

github.com/RoY3rS04/PhGo

Source

Issues

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/royers/phgo

1.0.0 2025-08-23 03:21 UTC

This package is not auto-updated.

Last update: 2026-01-10 19:30:43 UTC


README

A lightweight go-like HTTP library for PHP.

Installation

composer require royers/phgo

Usage

In order to be able to use this package you'll need to use these components:

  • A ServeMux object.
  • Your own functions which will be injected a Response and Request object by default.

Example on how to register a route

This simple example will show you how to access dynamic parameters and writing response headers.

Let's suppose we have a project that has this structure:

├──public
│   └──index.php
├──src
├──vendor
├──composer.json
├──blogs.json

public/index.php

<?php

require_once __DIR__ . "/../vendor/autoload.php";

use Royers\Http\Method;
use Royers\Http\{Request, Response, StatusCode};
use Royers\Http\ServeMux;

$mux = new ServeMux();

$mux->handleFunc(
    "/blogs/{id}",
    Method::Get,
    function (Response $w, Request $r) {

        $blogsPath = __DIR__ . "/../blogs.json";

        $blogsJson = file_get_contents($blogsPath);
        $blogs = json_decode($blogsJson, true);

        // PhGo doesn't validate data for you
        $blogId = filter_var(
            $r->dynamicParams["id"],
            FILTER_VALIDATE_INT,
            ['default' => 0]
        );

        if ($blogId <= 0) {

            $w->header()->set("Content-Type", "application/json");
            $w->writeHeader(StatusCode::BadRequest);

            echo json_encode(
                [
                    "msg" => "Blog id can't be lower than 1"
                ]
            );

            return;
        }

        $blog = array_find($blogs, fn (array $blog) => $blog['id'] === $blogId);

        if (!$blog) {

            $w->header()->set("Content-Type", "application/json");
            $w->writeHeader(StatusCode::NotFound);

            echo json_encode(
                [
                    "msg" => "Blog with id = {$blogId} not found"
                ]
            );

            return;
        }
        /*
        Notice that you must write all the headers you want to send
        with the header methods, i.e set, add, etc. before throwing any output.
        These headers will be send when you call the writeHeader method.
        */
        $w->header()->set("Content-Type", "application/json");
        $w->writeHeader(StatusCode::Ok);
        echo json_encode($blog);
    }
);

$mux->listen();

blogs.json

[
  {
    "id": 1,
    "title": "The Rise of Minimalist Web Design",
    "author": "Jane Doe",
    "date": "2025-01-12",
    "tags": ["design", "web", "minimalism"],
    "content": "Minimalist design continues to dominate the web, focusing on clarity and user experience."
  },
  {
    "id": 2,
    "title": "Understanding Pointers in C",
    "author": "John Smith",
    "date": "2025-01-20",
    "tags": ["programming", "C", "pointers"],
    "content": "Pointers are one of the most powerful features of C. In this article, we break down how they work."
  }
  // More blogs...
]