PHP code example of oliverlundquist / laravel-http-background
1. Go to this page and download the library: Download oliverlundquist/laravel-http-background library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oliverlundquist / laravel-http-background example snippets
// use it directly
HttpBg::get('https://httpbin.org/get');
HttpBg::head('https://httpbin.org/head');
HttpBg::post('https://httpbin.org/post', json_encode(['json' => 'payload']));
HttpBg::patch('https://httpbin.org/patch', json_encode(['json' => 'payload']));
HttpBg::put('https://httpbin.org/put', json_encode(['json' => 'payload']));
HttpBg::delete('https://httpbin.org/delete');
// or though a macro registered on the Laravel HTTP Client
Http::background()->get('https://httpbin.org/get');
Http::background()->head('https://httpbin.org/head');
Http::background()->post('https://httpbin.org/post', json_encode(['json' => 'payload']));
Http::background()->patch('https://httpbin.org/patch', json_encode(['json' => 'payload']));
Http::background()->put('https://httpbin.org/put', json_encode(['json' => 'payload']));
Http::background()->delete('https://httpbin.org/delete');
HttpBg::connectTimeout(10);
Http::background()->connectTimeout(10);
HttpBg::timeout(30);
Http::background()->timeout(30);
HttpBg::retry(3);
Http::background()->retry(3);
HttpBg::setRequestTag('webhook callback for id: 31');
Http::background()->setRequestTag('webhook callback for id: 31');
HttpBg::contentType('application/json');
Http::background()->contentType('application/json');
HttpBg::accept('application/json');
Http::background()->accept('application/json');
$contentType = 'application/json';
$accept = 'application/json';
HttpBg::withBody(json_encode(['json' => 'payload']), $contentType, $accept);
Http::background()->withBody(json_encode(['json' => 'payload']), $contentType, $accept);
HttpBg::queue()->get('https://httpbin.org/get');
Http::background()->queue()->get('https://httpbin.org/get');
$request = HttpBg::get('https://httpbin.org/get');
$request = Http::background()->get('https://httpbin.org/get');
$request->processIsRunning();
Event::listen(function (HttpBgRequestSending $event) { Log::info($event->request); });
Event::listen(function (HttpBgRequestSent $event) { Log::info($event->request); });
Event::listen(function (HttpBgRequestSuccess $event) { Log::info($event->requestId); });
Event::listen(function (HttpBgRequestFailed $event) { Log::info($event->requestId); });
Event::listen(function (HttpBgRequestTimeout $event) { Log::info($event->requestId); });
Event::listen(function (HttpBgRequestComplete $event) { Log::info($event->requestId); });
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Event::listen(function (HttpBgRequestSent $event) {
$request = $event->request;
Cache::put($request->id, $request->toArray());
});
Event::listen(function (HttpBgRequestFailed|HttpBgRequestTimeout $event) {
$requestId = $event->requestId;
$request = HttpBgRequest::newFromArray(Cache::get($requestId, []));
if (! $request->validateRequest()) {
return;
}
if ($request->maxAttempts > $request->attempts) {
HttpBg::send($request);
return;
}
Cache::forget($requestId);
Mail::to('[email protected]')
->send(new FailedWebHookMail($request->tag));
});
}
}