REMEMBER PHP

MINTE9
2REMEMBER




Last update:   23-07-2021

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
... 3 lines
˄˄˄
If PHP extension dom is missing:
 
sudo php install php-xml
sudo php install php-mbstring

composer create-project slim/slim-skeleton:dev-master composer-slim
... 3 lines
˄˄˄
 
http://localhost/composer-slim/public/index.php
... 1 lines
˄˄˄
 
// 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();
... 12 lines
˄˄˄
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/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

//Shows Hello, world
... 14 lines
˄˄˄
You can build an API using Slim Framework. To consume other API, you can use PHP Curl.
 CodeCopy
$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);
... 10 lines
˄˄˄

Questions    
No. 77   Guzzle
CARD
76

        A B C D E F
🔔
1/3