Modern Php / Slim Framework
Slim
Slim is a PHP micro framework. You quickly write simple yet powerful web applications and APIs.
cd /var/www/html
mkdir composer-slim
composer create-project slim/slim-skeleton:dev-master composer-slim
If PHP extension dom is missing:
sudo php install <span class='keyword_code'>php-xml</span>
sudo php install php-mbstring
composer create-project slim/slim-skeleton:dev-master composer-slim
http://localhost/composer-slim/public/index.php
// index.php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
Slim 4 - HttpNotFoundException
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->setBasePath("/composer-slim/public/<span class='keyword_code'>index.php</span>"); // look here
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
//http://localhost/composer-slim/public/index.php/hello/world
//Shows <span class='keyword_code'>Hello, world</span>
You can build an API using Slim Framework.
To consume other API, you can use PHP Curl.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://website.com/method");
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
// Fetch and return content, save it.
$raw_data = curl_exec($ch);
curl_close($ch);
// If the API is JSON, use json_decode.
$data = json_decode($raw_data);
var_dump($data);
Last update: 61 days ago