Skip to content

Commit

Permalink
Vercel livedemo added
Browse files Browse the repository at this point in the history
  • Loading branch information
eypsilon committed Mar 10, 2023
1 parent cee114c commit 7bdae0b
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 437 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
vendor/
.git/
.github/
.notes
composer.lock
composer.lock
.vercel
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This class is written to simplify the use of CURL in PHP using chained methods a

I needed a simple way to explore the Shopware API (v6.*) via http and the Bearer token authentication scheme, but haven't found, what i was looking for to do it, so i ended up with this one. It does nothing new at all, nothing exciting, just simple Requests to URLs and Endpoints like you would expect, but a bit prettier than CURL itself.

See [./tests/curler](./tests/curler/) directory for Examples.
See [./public/index.php](./public/index.php) for examples or check the livedemo on [Vercel](https://curler-eypsilon.vercel.app/).

```terminal
```bash
composer require eypsilon/curler
```

Expand Down
6 changes: 6 additions & 0 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// defined('CURLINFO_SCHEME')
defined('CURLINFO_SCHEME') or define('CURLINFO_SCHEME', 1048625);

require dirname(__DIR__) . '/public/index.php';
7 changes: 1 addition & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
}
},
"require": {
"php": ">=7.4"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.2-dev"
}
"php": ">=8.0"
}
}
5 changes: 0 additions & 5 deletions package.json

This file was deleted.

2 changes: 2 additions & 0 deletions public/app.responder.receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Curler Responder, let's say 'https://example.com/restricted/' is .htaccess protected and returns
*/

use Many\Http\Curler;

$rspns = [
'auth_type' => $_SERVER['AUTH_TYPE'] ?? 'error',
'header' => getallheaders(),
Expand Down
122 changes: 92 additions & 30 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php \error_reporting(E_ALL);
<?php error_reporting(E_ALL);

/**
* To use this example, copy the './tests/curler'
* directory to where composers './vendor' directory is
*
* For demo purposes only
*
* $ ~/terminal/in/./tests/curler
* php -S localhost:8000
* http://localhost:8000
*/

use Many\Http\Curler;
use Many\Exception\AppCallbackException;

require_once \dirname(__DIR__) . '/vendor/autoload.php';
require_once \dirname(__FILE__) . '/callback.functions.php';
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once __DIR__ . '/callback.functions.php';

/** Get file content in a browser */
if ('self' === ($_GET['check'] ?? null))
{
exit(highlight_file(__FILE__, true));
}

/** @var string (optional) default URL */
$defaultUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb';
Expand All @@ -30,12 +31,13 @@
];

/** @var string File with json content */
$jsonFile = 'https://raw.githubusercontent.com/eypsilon/Curler/master/tests/curler/www.example.json';
$jsonFile = 'https://raw.githubusercontent.com/eypsilon/MycroBench/master/composer.json';

/** @var array some URLs to load */
$loadUrls = [
'http://example.com/',
'https://raw.githubusercontent.com/eypsilon/browser-reload/master/LICENSE',
'https://github.com/eypsilon/MycroBench',
];


Expand All @@ -62,11 +64,11 @@
'default_callback' => [],
]);


/**
* @var Curler extended example with multiple callbacks (order matters)
*/
try {
$curled['curler_info'] = '<b>Loads the content of a JSON file and executes some callback functions in a row</b> <small>(7 to be specific)</small>';
$curled['curler'] = (new Curler)
// ->disableDefaultUrl()
// ->responseOnly()
Expand Down Expand Up @@ -109,13 +111,14 @@
/**
* @var array Load multiple URLs
*/
$curled['load_urls_info'] = '<b>Loads a handful of URLs and truncates the response</b>';
foreach($loadUrls as $i => $url) {
try {
$host = \parse_url((string) $url, PHP_URL_HOST);
$host = parse_url((string) $url, PHP_URL_HOST);
$curled['load_urls'][] = (new Curler)
->htmlChars()
->callback('trim')
->callback('substr', 0, 17)
->callback('substr', 0, 20)
->callback('appendString', '' . Curler::dateMicroSeconds())
->get($url);
} catch(AppCallbackException $e) {
Expand All @@ -135,47 +138,106 @@

$c = new Curler;
$printImages = [];
foreach($imgList as $img) {
$curled['loaded_images_info'] = '<b>The images displayed above, the responses are truncated</b>';

foreach($imgList as $img)
{
try {
if ($src = $c->get($img)) {
$curled['images'][] = $src;
$printImages[] = \sprintf('<img src="%s" alt="" style="height:125px" />', $src['response']);
if ($src = $c->get($img))
{
$printImages[] = sprintf('<img src="%s" alt="%s" style="height:130px" />', $src['response'], $img);
$src['response'] = substr($src['response'], 0, 35) . '';
$curled['loaded_images'][] = $src;
}
} catch(AppCallbackException $e) {
$curled['exception']['images'][$img] = $e->getMessage();
}
}

$url = $_GET['url'] ?? null;
$setUrl = is_string($url) ? $url : 'https://mycro-bench.vercel.app/';

/**
* Template Engin © 1994 eypsilon
* @Template\Engin © 1994 eypsilon
*/
?><!DOCTYPE html>
<html><head><meta charset="utf-8" />
<title>Many/Curler | local-dev-many-title</title>
<html>
<head>
<meta charset="utf-8" />
<title>Many\Curler | local-dev-many-title</title>
<meta name="description" content="Many/Curler Example Page" />
</head><body>
<style>
header {text-align: center;}
header h1 {margin: 0;}
header p {margin: 5px 0;}
fieldset {margin: 15px 0; padding: 10px; display: flex; background: #f5f5f5; border-color: #fff;}
input {width: 100%; border-width: 1px 0;}
[type=button] {pointer-events: none; color: #999;}
input, button {display: block; padding: 6px 10px;}
pre {margin: 1.5em 0; white-space: pre-wrap;}
hr {margin: 1em 0;}
</style>
</head>
<body>
<header>
<h1><a href="/">Many\Curler</a></h1>
<p>another one curls the dust</p>
</header>
<form action="" method="get">
<fieldset>
<button type="button">test</button>
<input type="text" name="url" value="<?= htmlspecialchars($setUrl) ?>" placeholder="Enter a URL" />
<button type="submit">Get</button>
</fieldset>
</form>
<?php

// live curler
if (is_string($url) AND false === filter_var($url, FILTER_VALIDATE_URL))
{
printf('<h2>Many\Curler\Live</h2><pre>FILTER_VALIDATE_URL_FAILED %s</pre><hr />'
, htmlspecialchars($url)
);
} elseif (is_string($url) AND false !== filter_var($url, FILTER_VALIDATE_URL)) {
printf('<h2>Many\Curler\Live</h2><pre>%s</pre><hr />',
(new Curler)
->disableDefaultUrl()
->responseOnly()
->htmlChars()
->get($url)
);
}

// out
\printf('%s<pre>%s</pre>'
, \implode(PHP_EOL, $printImages) // print images
, \print_r(\array_merge($curled, [
printf('%s<h2>Some requests</h2><pre>%s</pre>'
, implode(PHP_EOL, $printImages) // print images
, print_r(array_merge($curled, [
'getCurlCount' => Curler::getCurlCount(),
'getCurlTrace' => Curler::getCurlTrace(),
'getCurlTrace' => array_map(
function($e) use($defaultUrl) {
return str_replace($defaultUrl, '{replaced_url} …', $e);
}, Curler::getCurlTrace()
),
'getConfig' => Curler::getConfig(),
'getOptions' => Curler::getOptions(),
]), true)
);

// responder/receiver example
printf('<hr /><h3>Responder/Receiver Example</h3> %s <hr />
<p><a href="?check=self">Check current file</a></p>'
, highlight_file(__DIR__ . '/app.responder.receiver.php', true)
);

// advanced benchmarks
\printf("<pre>start: %s\nend: %s\ndiff: %s\nmem: %s\npeak: %s</pre>"
printf("<hr /><pre>start: %s\nend: %s\ndiff: %s\nmem: %s\npeak: %s</pre>"
, $started = Curler::dateMicroSeconds($_SERVER['REQUEST_TIME_FLOAT'], 'H:i:s.u')
, $current = Curler::dateMicroSeconds(null, 'H:i:s.u') // current microtime(true)
, Curler::dateMicroDiff($started, $current, '%s.%f')
, Curler::readableBytes(\memory_get_usage())
, Curler::readableBytes(\memory_get_peak_usage())
, Curler::readableBytes(memory_get_usage())
, Curler::readableBytes(memory_get_peak_usage())
);

?>
<hr /><div style="text-align:center">
<h1>Many/Curler</h1>
<p>another one curls the dust</p></div>
</body></html>
</body>
</html>
101 changes: 0 additions & 101 deletions tests/curler/README.md

This file was deleted.

Loading

1 comment on commit 7bdae0b

@vercel
Copy link

@vercel vercel bot commented on 7bdae0b Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

curler – ./

curler-eypsilon.vercel.app
curler.vercel.app
curler-git-master-eypsilon.vercel.app

Please sign in to comment.