Skip to content

Instantly share code, notes, and snippets.

@kn0ll
Last active October 3, 2022 13:08
Show Gist options
  • Save kn0ll/1061243 to your computer and use it in GitHub Desktop.
Save kn0ll/1061243 to your computer and use it in GitHub Desktop.
an asynchronous http library for php.
<?php
namespace clutch;
class Request {
/**
* The url of the request
* @var string
*/
public $url;
/**
* The POST or GET data to be sent to the server
* @var array
*/
public $data;
/**
* The HTTP method of the request
* @var string
*/
public $method;
/**
* The curl handle representing the request
* @var curl handle
*/
public $ch;
/**
* Generates a curl handle representing the HTTP request
* and initializes the members of the Request object
* @param url
* @param data
* @param method
*/
function __construct($url, $data, $method) {
// http://www.php.net/manual/en/function.http-build-query.php#102324
$qs = http_build_query($data, "", "&");
$ch = curl_init();
if ($method == "GET") {
curl_setopt($ch, CURLOPT_URL, "{$url}?{$qs}");
} else if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
curl_setopt($ch, CURLOPT_URL, "{$url}");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->url = $url;
$this->data = $data;
$this->method = $method;
$this->ch = $ch;
}
}
class Response {
/**
* The body of the HTTP response
* @var string
*/
public $body;
/**
* The HTTP headers of the response
* @var string
*/
public $headers;
/**
* Initialize the Response properties
* @param body
* @param headers
*/
function __construct($body, $headers) {
$this->body = $body;
$this->headers = $headers;
}
}
class Async {
/**
* A list of all Requests to be made
* @var array
* @access private
*/
private $requests = array();
/**
* Adds a request to the list of Async request
* @param url
* @param data
* @param method
* @see Request
*/
function add($url, $data = array(), $method = "GET") {
$this->requests[] = new Request($url, $data, $method);
}
/**
* Executes all the calls added to it's stack
* and returns the associated Responses
* @return Response
* @see Response
*/
function send() {
$responses = array();
$cmh = curl_multi_init();
$still_running = null;
foreach ($this->requests as $request) {
curl_multi_add_handle($cmh, $request->ch);
}
do {
$mrc = curl_multi_exec($cmh, $still_running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($still_running && $mrc == CURLM_OK) {
if (curl_multi_select($cmh) != -1) {
do {
$mrc = curl_multi_exec($cmh, $still_running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($this->requests as $request) {
$body = curl_multi_getcontent($request->ch);
$responses[] = new Response($body, array(
"http_code" => curl_getinfo($request->ch, CURLINFO_HTTP_CODE)
));
curl_multi_remove_handle($cmh, $request->ch);
}
curl_multi_close($cmh);
return $responses;
}
}
<?php
require "clutch.php"
$ws = new clutch\Async();
$ws->add("http://localhost/users", array("userId" => "test_add"), "POST");
$ws->add("http://localhost/users/friends", array("userId" => "test_add"));
$responses = $ws->send();
echo responses[0]->body;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment