1. Go to this page and download the library: Download symplely/hyper 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/ */
symplely / hyper example snippets
/**
* @see https://github.com/amphp/artax/blob/master/examples/6-parallel-requests.php
*/
include 'vendor/autoload.php';
use Async\Coroutine\Exceptions\Panicking;
// An infinite loop example task, this will output on all task executions, pauses, stalls, or whatever.
function lapse() {
$i = 0;
while(true) {
$i++;
print $i.'.lapse ';
yield;
}
}
// An initial function is quest()` starts an new background task, does not block or wait.
// the `array` will be passed to `fetch();` for resolution.
foreach ($uris as $uri) {
$uriId[] = yield \request(\http_get($uri));
}
// Executions here will pause and wait for each request to receive an response,
// all previous tasks started will continue to run, so you will see output only
// from the `lapse` task here until the foreach loop starts.
// Doing `\fetchOptions(1);` before this statement will wait for only 1 response.
$bodies = yield \fetch($uriId);
foreach ($bodies as $id => $result) {
$uri = \response_meta($result, 'uri');
// will pause execution of loop until full body is received, only `lapse` task will still output
$body = yield \response_body($result);
print \EOL."HTTP Task $id: ". $uri. " - " . \strlen($body) . " bytes" . \EOL.\EOL;
}
} catch (Panicking $error) {
echo 'There was a problem: '.$error->getMessage();
}
// display the collected stats logs
\print_defaultLog();
yield \http_closeLog();
// if you don't the infinite loop will continue it's output.
yield \shutdown();
}
// Coroutine code needs to be bootstrapped.
// The function called `MUST` have at least one `yield` statement.
\coroutine_run(\main());
const SYMPLELY_USER_AGENT = 'Symplely Hyper PHP/' . \PHP_VERSION;
// Content types for header data.
const HTML_TYPE = 'text/html';
const OCTET_TYPE = 'application/octet-stream';
const XML_TYPE = 'application/xml';
const PLAIN_TYPE = 'text/plain';
const MULTI_TYPE = 'multipart/form-data';
const JSON_TYPE = 'application/json';
const FORM_TYPE = 'application/x-www-form-urlencoded';
/**
* This function works similar to coroutine `await()`
*
* Takes an `request` instance or `yield`ed coroutine of an request.
* Will immediately return an `int` HTTP task id, and continue to the next instruction.
* Will resolve to an Response instance when `fetch()`
*
* - This function needs to be prefixed with `yield`
*/
yield \request();
/**
* Run awaitable HTTP tasks in the requests set concurrently and block
* until the condition specified by count.
*
* This function works similar to `gatherWait()`.
*
* Controls how the `wait/fetch` functions operates.
* `await()` will behave like **Promise** functions `All`, `Some`, `Any` in JavaScript.
*
* - This function needs to be prefixed with `yield`
*/
yield \fetch_await($requests, $count, $exception, $clearAborted);
/**
* This function works similar to coroutine `gather()`
*
* Takes an array of request HTTP task id's.
* Will pause current task and continue other tasks until
* the supplied request HTTP task id's resolve to an response instance.
*
* - This function needs to be prefixed with `yield`
*/
yield \fetch(...$requests);
/**
* This function works similar to `cancel_task()`
*
* Will abort the supplied request HTTP task id and close stream.
*
* - This function needs to be prefixed with `yield`
*/
yield \request_abort($httpId);
/**
* This function is automatically called by the http_* functions.
*
* Creates and returns an `Hyper` instance for the global HTTP functions by.
*/
\http_instance($tag);
/**
* Clear & Close the global `Hyper`, and `Stream` Instances by.
*/
\http_clear($tag);
/**
* Close and Clear `ALL` global Hyper function, Stream instances.
*/
\http_nuke();
/**
* Make a GET request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_get($tagUri, ...$authorizeHeaderOptions);
/**
* Make a PUT request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_put($tagUri, ...$authorizeHeaderOptions);
/**
* Make a POST request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_post($tagUri, ...$authorizeHeaderOptions);
/**
* Make a PATCH request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_patch($tagUri, ...$authorizeHeaderOptions);
/**
* Make a DELETE request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_delete($tagUri, ...$authorizeHeaderOptions);
/**
* Make a OPTIONS request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_options($tagUri, ...$authorizeHeaderOptions);
/**
* Make a HEAD request, will pause current task, and
* continue other tasks until an response is received.
*
* - This function needs to be prefixed with `yield`
*/
yield \http_head($tagUri, ...$authorizeHeaderOptions);
/**
* This function is automatically called by the http_* functions.
*
* Set global functions response instance by.
*/
\response_set($response, $tag);
/**
* This function is automatically called by the response_* functions.
*
* Return current global functions response instance by.
*/
\response_instance($tag);
/**
* Clear and close global functions response/stream instance by.
*/
\response_clear($tag);
/**
* Close and Clear `ALL` global functions response instances.
*/
\response_nuke();
/**
* Is response from an successful request?
* Returns an `bool` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_ok($tag);
/**
* Returns response reason phrase `string` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_phrase($tag);
/**
* Returns response status code `int` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_code($tag);
/**
* Check if response has header key by.
* Returns `bool` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_has($tag, $header);
/**
* Retrieve a response value for header key by.
* Returns `string` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_header($tag, $header);
/**
* returns response FULL body.
*
* - This function needs to be prefixed with `yield`
*/
yield \response_body($tag);
/**
* Returns `string` of response metadata by key.
*/
\response_meta($tag, $key);
/**
* Check if response body been read completely by.
* Returns `bool` or NULL, if not ready.
*
* This function can be used in an loop control statement,
* which you will `yield` on `NULL`.
*/
\response_eof($tag);
/**
* returns response STREAM body.
*
* - This function needs to be prefixed with `yield`
*/
yield \response_stream($tag, $size);
/**
* returns response JSON body.
*
* - This function needs to be prefixed with `yield`
*/
yield \response_json($tag, $assoc);
/**
* returns response XML body.
*
* - This function needs to be prefixed with `yield`
*/
yield \response_xml($tag, $assoc);
use Async\Request\Hyper;
function main() {
$http = new Hyper;
$response = yield $http->get("https://www.google.com");
$response = yield $http->post("https://example.com/search", ["Form data"]));
$response = $http->get("https://api.example.com/v1/books");
echo $response->getStatusCode(); // 200
echo $response->getReasonPhrase(); // OK
// The body is return asynchronous in an non-blocking mode,
// and as such needs to be prefixed with `yield`
$body = yield $response->getBody()->getContents();
}
// All coroutines/async/await needs to be enclosed/bootstrapped
// in one `main` entrance routine function to properly execute.
// The function `MUST` have at least one `yield` statement.
\coroutine_run(\main());
use Async\Request\Uri;
use Async\Request\Request;
use Async\Request\Hyper;
function main() {
// Build Request message.
$request = new Request;
$request = $request
->withMethod("get")
->withUri(Uri::create("https://www.google.com"))
->withHeader("Accept-Language", "en_US");
$http = new Hyper;
// Send the Request.
// Pauses current/task and send request,
// will continue next to instruction once response is received,
// other tasks/code continues to run.
$response = yield $http->sendRequest($request);
}
// All coroutines/async/await needs to be enclosed/bootstrapped
// in one `main` entrance routine function to properly execute.
// The function `MUST` have at least one `yield` statement.
\coroutine_run(\main());
use Async\Request\Body;
use Async\Request\Hyper;
function main() {
$book = [
"title" => "Breakfast Of Champions",
"author" => "Kurt Vonnegut",
];
$http = new Hyper;
yield $http->post("https://api.example.com/v1/books", [Body::JSON, $book]);
// Or
yield $http->post("https://api.example.com/v1/books", new Body(Body::JSON, $book));
// Or
yield $http->post("https://api.example.com/v1/books", Body::create(Body::JSON, $book));
// Otherwise the default, will be submitted in FORM format of `application/x-www-form-urlencoded`
yield $http->post("https://api.example.com/v1/books", $book);
}
// All coroutines/async/await needs to be enclosed/bootstrapped
// in one `main` entrance routine function to properly execute.
// The function `MUST` have at least one `yield` statement.
\coroutine_run(\main());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.