Phetch is the missing 80% use case HTTP client for PHP. Phetch did not invent anything new, rather it is borrowing and building these ideas & desires:
- API is natural, comfortable, and pleasant to use
- A Zttp like client without the Guzzle dependency
- As approachable as HTTPie
- Json by default / Sensible defaults
- Lightweight wrapper of PHP stream (http, ssl) functionality
- Has shallow stack traces
- Service container friendly
$ composer require ralphschindler/phetch
// simple call to the Github API
$resp = Phetch\Phetch::withBearerAuth('abcdefghijklmnopqrstuvwxyz0123456789')
->get('https://api.github.com/repos/ralphschindler/phetch/issues', ['state' => 'all']);
$resp->json(); // the response body as an array
$response = Phetch\Phetch::request()->post('https://api.github.com/repos/ralphschindler/phetch/issues', [
'title' => 'My Issue',
'body' => 'This is the body to my issue',
]);
$response = Phetch\Phetch::request()->patch('https://api.github.com/repos/ralphschindler/phetch/issues/1', [
'title' => 'My Issue Updated Title!',
]);
// locking github issues requires a special accept header
$response = Phetch\Phetch::request()->delete('https://api.github.com/repos/ralphschindler/phetch/issues/1/lock',
['headers' => ['Accept' => 'application/vnd.github.sailor-v-preview+json']]
);
$req = Phetch\Phetch::withBaseUrl('https://api.github.com');
$respGet = $request->get(...);
$respPatch = $request->patch(...);
$page = Phetch\Phetch::withoutVerifying()->get('https://IDidntUpdateMyCert.org');
$page = Phetch\Phetch::withoutRedirecting()->get('https://AUrlThatRedirects.com/place');
$page = Phetch\Phetch::withBasicAuth($username, $password)->get('https://AUrlThatRedirects.com/place');
$page = Phetch\Phetch::withBearerAuth($token)->get('https://AUrlThatRedirects.com/place');
Often times you may want to pre-configure a request to be used with shared settings in different contexts. Additionally, you may want to build a web-service specific client.
Creating a service allows you to configure and share a PendingRequest
object
with as much boilerplate as necessary for your app to talk to a particular
service. ($container
is assumed to be some kind of service container.)
$githubWebService = Phetch\Phetch::createService(function ($pendingRequestPrototype) {
$pendingRequestPrototype->withHeaders(['User-Agent' => 'My Applications Http Client v1.0.0'])
->withBaseUrl('https://api.github.com')
->withBearerAuth('abcdefghijklmnopqrstuvwxyz0123456789');
});
$container->share('github-web-service', $githubWebService);
The PhetchService
now contains your prototypical PendingRequest
object that is preconfigured for use everywhere in your application. Each time
you call $service->request()
, you will get a cloned/fresh PendingRequest
object that you can interact with. State changes to this new object will
not affect the service's pre-configured object:
/** @var \Phetch\PhetchService $github */
$github = $container->get('github-web-service');
// request() will always produce a fresh & pre-configured \Phetch\PendingRequest
$resp = $github->request()
->get('/repos/ralphschindler/phetch/issues', ['state' => 'closed']);
- httpie command parsing
- command line phetch client
- curl adapter (good open source contribution would be nice!)