mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
added function for http requests
This commit is contained in:
parent
325a1bcaf0
commit
0937042e07
10
README.md
10
README.md
@ -195,7 +195,7 @@ Name | Example | Example result | Description
|
||||
|
||||
### Database functions
|
||||
|
||||
This is just a simple wrapper for [PDO](https://www.php.net/manual/en/book.pdo.php).
|
||||
This is a simple wrapper for [PDO](https://www.php.net/manual/en/book.pdo.php).
|
||||
|
||||
Name | Example | Example result | Description
|
||||
----------- | ------- | -------------- | -----------
|
||||
@ -207,6 +207,14 @@ db-trans | `(db-trans d)` | `true` | Start a transaction.
|
||||
db-commit | `(db-commit d)` | `true` | Commit a transaction.
|
||||
db-rollback | `(db-rollback d)` | `true` | Roll back a transaction.
|
||||
|
||||
### Http functions
|
||||
|
||||
This is a simple wrapper for [cURL](https://www.php.net/manual/en/book.curl.php).
|
||||
|
||||
Name | Example | Example result | Description
|
||||
----------- | ------- | -------------- | -----------
|
||||
http | `(http "POST" "http://example.com/" (to-json {"key":"value"}) {"Content-Type":"application/json"})` | `{"status":200 "body":"" "headers":{}}` | Perform a HTTP request. First argument is the HTTP method, second is URL, third is request body and fourth is headers as a hash-map. The function returns a hash-map which contains keys `status`, `body` and `headers`.
|
||||
|
||||
### IO functions
|
||||
|
||||
Name | Example | Example result | Description
|
||||
|
72
src/Lib/Http.php
Normal file
72
src/Lib/Http.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace MadLisp\Lib;
|
||||
|
||||
use MadLisp\CoreFunc;
|
||||
use MadLisp\Env;
|
||||
use MadLisp\Hash;
|
||||
use MadLisp\MadLispException;
|
||||
|
||||
class Http implements ILib
|
||||
{
|
||||
public function register(Env $env): void
|
||||
{
|
||||
$env->set('http', new CoreFunc('http', 'Perform a HTTP request.', 2, 4,
|
||||
function (string $method, string $url, ?string $requestBody = null, ?Hash $requestHeaders = null) {
|
||||
$ch = curl_init($url);
|
||||
|
||||
$method = strtoupper($method);
|
||||
if ($method == 'HEAD') {
|
||||
curl_setopt($ch, CURLOPT_NOBODY, true);
|
||||
} elseif ($method != 'GET') {
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||
}
|
||||
|
||||
if ($requestBody !== null) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
|
||||
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
|
||||
if ($requestHeaders !== null) {
|
||||
$h = [];
|
||||
foreach ($requestHeaders->getData() as $key => $val) {
|
||||
$h[] = "$key: $val";
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
|
||||
}
|
||||
|
||||
// for debugging
|
||||
// curl_setopt($ch, CURLOPT_VERBOSE, true);
|
||||
|
||||
$responseHeaders = [];
|
||||
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$responseHeaders) {
|
||||
$parts = explode(':', $header);
|
||||
if (count($parts) == 2) {
|
||||
$key = trim($parts[0]);
|
||||
$val = trim($parts[1]);
|
||||
$responseHeaders[$key] = $val;
|
||||
}
|
||||
return strlen($header);
|
||||
});
|
||||
|
||||
$responseBody = @curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch) !== 0) {
|
||||
throw new MadLispException('HTTP error: ' . curl_error($ch));
|
||||
}
|
||||
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
return new Hash([
|
||||
'status' => $status,
|
||||
'body' => $responseBody,
|
||||
'headers' => new Hash($responseHeaders)
|
||||
]);
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
@ -45,6 +45,7 @@ class LispFactory
|
||||
(new Lib\Collections())->register($env);
|
||||
(new Lib\Compare())->register($env);
|
||||
(new Lib\Database())->register($env);
|
||||
(new Lib\Http())->register($env);
|
||||
(new Lib\IO())->register($env);
|
||||
(new Lib\Json())->register($env);
|
||||
(new Lib\Math())->register($env);
|
||||
|
Loading…
Reference in New Issue
Block a user