hasanparasteh / async-request
Use async curl in reactphp project as easy as possible
Installs: 2 366
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: >=8.0.0
- clue/socks-react: ^1.4.0
- react/http: ^1.8.0
- react/promise-timer: ^1.9.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
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
Quickstart Example
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 } });
GET
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")
POST
It's just like the GET
request but it sends the paramethers as a json encoded raw!
$request->get("endpoint", ['paramName' => 'paramValue' ], ['headerName'=>'headerValue']);
PUT
It's exactly like the POST
.
$request->put("endpoint")
PATCH
It's exactly like the POST
.
$request->patch("endpoint")
DELETE
It's exactly like the POST
.
$request->delete("endpoint")