minte9
LearnRemember



Slim

Slim is a PHP micro framework that helps you write simple yet powerful web apps and APIs. It is recommend to install Slim Framework with the Composer dependency manager. The easiest way to start working with Slim is to create a project using Slim-Skeleton.
 
composer require slim/slim "^4.0"

sudo php install php-xml
sudo php install php-mbstring

cd /var/www/html
mkdir [my-app-name]

composer create-project slim/slim-skeleton:dev-master [my-app-name]
php -S localhost:8080 -t public

# http://localhost:8080/my-app-name/public/index.php

Entry point

Create an index.php file. This file will serve as the entry point of your Slim application.
 
# 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->setBasePath("/composer-slim/public/index.php"); // 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
# Hello, world

Consumer

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: 349 days ago