This package will help you to send any request to any server in an asynchronous way! Just follow the instructions in order to install and setup the async curl package.
> composer require hasanparasteh/async-request
This is the simplest way to do a GET
request. The results will be in a callable function which has 3 major data in it.
- result:
bool
=> represent that curl is successful or not - code:
int
=> http status code - body:
array
=> json decoded array which server returned - error:
string
=> description of the curl error
$request = new AsyncRequest("https://reqres.in");
$request->get("/api/users", ["page" => 2])->then(function ($result) {
if (!$result['result'])
echo "Curl Error cause {$result['error']}";
else
switch ($result['code']) {
case 200:
echo "Server Response 200 With " . json_encode($result['body'], 128);
break;
case 400:
echo "Server Response 400";
break;
case 500:
echo "Server Response 500";
break;
// .. and any other response Code
}
});
if you need to pass any query params just sends the as an array to the second argument and if you need to add any header just pass it in the third argument as an array.
$request->get("endpoint")
It's just like the GET
request but it sends the paramethers as a json encoded raw!
$request->get("endpoint", ['paramName' => 'paramValue' ], ['headerName'=>'headerValue']);
It's exactly like the POST
.
$request->put("endpoint")
It's exactly like the POST
.
$request->patch("endpoint")
It's exactly like the POST
.
$request->delete("endpoint")