added function for http requests

This commit is contained in:
Pekka Laiho 2020-06-20 10:33:15 +07:00
parent 325a1bcaf0
commit 0937042e07
3 changed files with 82 additions and 1 deletions

View File

@ -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
View 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)
]);
}
));
}
}

View File

@ -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);