Search by

ngawidev / netstream

ngawidev

Library For Http Request Without Curl

Package info

github.com/ngawidev/NetStream

pkg:composer/ngawidev/netstream

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-09-20 19:23 UTC

This package is auto-updated.

Last update: 2026-09-20 19:29:17 UTC


README

Language / Bahasa: English | Indonesia

Fluent HTTP client for PHP without cURL — built only on PHP streams (stream_context + fopen). Compatible with PHP >= 7.4, zero required dependencies.

English

Installation

composer require ngawidev/netstream

Quick start

use Ngawidev\Netstream\NetStream;
use Ngawidev\Netstream\Client;

// One-liner (argument style)
$res = NetStream::get('https://httpbin.org/get', ['page' => 1]);
$res = NetStream::post('https://httpbin.org/post', ['name' => 'Andi']);

// Configure first, execute later (verb args optional; args win over presets)
$res = NetStream::setPath('https://httpbin.org/post')->setJson(['a' => 1])->post();

// Reusable client (persistent cookies, shared config)
$client = new Client(['base_url' => 'https://api.example.com', 'timeout' => 10]);
$res = $client->withToken('xxx')->get('/users', ['active' => 1]);

echo $res->status();   // 200
var_dump($res->ok());  // true
print_r($res->json()); // array; dot-notation supported: $res->json('data.user')
echo $res->body();

Features

Feature Example
Verbs get/post/put/patch/delete/head/options/send
Auth withToken('x'), withBasicAuth('u','p')
Query & body setQuery(), setJson(), setForm(), withQuery() (merge)
HTTP proxy withProxy('127.0.0.1', 8080), withProxyAuth(), withoutProxy([...])
Cookie jar auto store + send; withCookies(), Response::cookies()
Retry retry(3, 200) + allowUnsafeRetry() + retryWhen(fn)
Download sink('file.zip')->get($url) memory-efficient
Upload attach('photo', $path)->field('note', 'x')->post()
Debug file enableDebug('debug_'.date('Ymd_His').'.log') (auth + cookies masked)
Response status/ok/failed/headers/header/hasHeader/json/throw_
TLS/redirect withoutVerifying(), withCaBundle(), followRedirects()
Limits maxResponseBytes(1048576), 32MB attach guard

See examples/ for a demo of each feature: basic, client, fluent, proxy, proxy_ipv6, cookies, headers, error, debug, retry, download, upload.

Testing

composer test            # local unit tests (no network)
composer test-network    # includes live tests against httpbin.org

Requires composer install first (pulls dev phpunit/phpunit). If your network is restricted and composer fails to download dev-deps, download phpunit-9.phar from https://phar.phpunit.de into tools/ (already gitignored), then:

php tools/phpunit.phar --testsuite unit
php tools/phpunit.phar --testsuite network

Security notes

  • Only http/https URLs are opened (file://, php://, ... are rejected).
  • Header values are stripped of CR/LF (no header injection).
  • Redirects are followed manually (max 5): Authorization/Cookie are dropped when the host changes or https downgrades to http; cookies from intermediate hops are still stored in the jar.
  • user:pass@ credentials are redacted in logs and exceptions. Query strings are logged as-is for debugging — keep secrets out of URLs when sharing logs.
  • Response::headers() keys are lowercased (header names are case-insensitive).

License

MIT — see LICENSE.

Indonesia

Fluent HTTP client untuk PHP tanpa cURL — hanya memakai stream bawaan PHP (stream_context + fopen). Kompatibel PHP >= 7.4, tanpa dependency wajib.

Instalasi

composer require ngawidev/netstream

Cara cepat

use Ngawidev\Netstream\NetStream;
use Ngawidev\Netstream\Client;

// Satu baris (gaya argumen)
$res = NetStream::get('https://httpbin.org/get', ['page' => 1]);
$res = NetStream::post('https://httpbin.org/post', ['name' => 'Andi']);

// Set dulu, eksekusi belakangan (argumen verb opsional; argumen menang atas preset)
$res = NetStream::setPath('https://httpbin.org/post')->setJson(['a' => 1])->post();

// Client dipakai ulang (cookie persist, config dishare)
$client = new Client(['base_url' => 'https://api.example.com', 'timeout' => 10]);
$res = $client->withToken('xxx')->get('/users', ['active' => 1]);

echo $res->status();   // 200
var_dump($res->ok());  // true
print_r($res->json()); // array; dukung dot-notation: $res->json('data.user')
echo $res->body();

Fitur

Fitur Contoh
Verbs get/post/put/patch/delete/head/options/send
Auth withToken('x'), withBasicAuth('u','p')
Query & body setQuery(), setJson(), setForm(), withQuery() (merge)
Proxy HTTP withProxy('127.0.0.1', 8080), withProxyAuth(), withoutProxy([...])
Cookie jar otomatis simpan + kirim; withCookies(), Response::cookies()
Retry retry(3, 200) + allowUnsafeRetry() + retryWhen(fn)
Download sink('file.zip')->get($url) hemat memori
Upload attach('foto', $path)->field('ket', 'x')->post()
Debug file enableDebug('debug_'.date('Ymd_His').'.log') (auth + cookie disamarkan, debugShowCookies(true) untuk opt-out)
Response status/ok/failed/headers/header/hasHeader/json/throw_
TLS/redirect withoutVerifying(), withCaBundle(), followRedirects()
Limits maxResponseBytes(1048576), guard attach 32MB

Lihat examples/ untuk demo tiap fitur: basic, client, fluent, proxy, proxy_ipv6, cookies, headers, error, debug, retry, download, upload.

Testing

composer test            # unit lokal (tanpa jaringan)
composer test-network    # termasuk tes live ke httpbin.org

Butuh composer install dulu (menarik phpunit/phpunit dev). Bila jaringan dibatasi dan composer gagal mengunduh dev-deps, unduh phpunit-9.phar dari https://phar.phpunit.de ke tools/ (sudah di-gitignore) lalu:

php tools/phpunit.phar --testsuite unit
php tools/phpunit.phar --testsuite network

Catatan keamanan

  • Hanya URL http/https yang dibuka (file://, php://, ... ditolak).
  • Nilai header dibersihkan dari CR/LF (anti injeksi header).
  • Redirect diikuti manual (maks 5): Authorization/Cookie dicopot saat ganti host atau downgrade httpshttp; cookie hop perantara tetap ke jar.
  • Kredensial user:pass@ disamarkan di log dan exception. Query string tercatat apa adanya untuk debug — jangan taruh sekret di URL bila log dibagikan.
  • Key Response::headers() lowercase (nama header case-insensitive).

Lisensi

MIT — lihat LICENSE.