├── README.md ├── Vagrantfile ├── application ├── .htaccess ├── composer.json └── index.php └── bootstrap.sh /README.md: -------------------------------------------------------------------------------- 1 | # simple-php-api (in under 40 lines of code) 2 | 3 | An extremely simple API (with authentication) example, written in PHP 4 | on top of the excellent Slim micro framework. 5 | This repo shows the "server", meaning the part that offers the data. 6 | There's another repo (https://github.com/panique/simple-php-api-client) 7 | showing a "client", meaning the part that GETS the data. 8 | 9 | ## What's inside ? 10 | 11 | The entire API is inside index.php, but it's using the micro framework 12 | Slim and an authentication module. The .htaccess creates these 13 | nice-looking URLs like /api/test, that's actually part of Slim. 14 | 15 | ## How to install 16 | 17 | #### Manual installation: 18 | 19 | Put index.php, composer.json and .htaccess from the *application* folder 20 | inside your server. As this API uses "beautiful URLs" (like 21 | /api/stuff, not index.php?path=api&...), make sure you have mod_rewrite 22 | activated (when using Apache) and set your virtualhost to 23 | "AllowOverride All". If you don't know what this means, better use the 24 | automatic installation: 25 | 26 | #### Automatic installation: 27 | 28 | For simple setup, this project comes with a full installer which sets 29 | up a Ubuntu 14.04 LTS Vagrant box and installs Apache, PHP5, Composer 30 | etc. and sets up the application. To do so, go to where the 31 | *Vagrantfile* is and do a "vagrant up" on the console. Your application 32 | is then reachable under 192.168.33.44 33 | 34 | ## How to use 35 | 36 | After installing, check the API which a browser, when using the 37 | auto-installer you can do that under http://192.168.33.44/api/test, 38 | you'll be asked to enter your login (username: demouser, password: 123) 39 | and get back a JSON string. Nice! 40 | 41 | ## Real-world use 42 | 43 | For real use, you or somebody else will probably access this API 44 | remotely with a client, plase have a look at my other repo 45 | https://github.com/panique/simple-php-api-client which shows exactly 46 | how to access this API from the outside, also in PHP. 47 | 48 | ## License 49 | 50 | MIT, do whatever you want with it. 51 | 52 | The MIT License (MIT) 53 | 54 | Copyright (c) 2016 panique 55 | 56 | Permission is hereby granted, free of charge, to any person obtaining a copy 57 | of this software and associated documentation files (the "Software"), to deal 58 | in the Software without restriction, including without limitation the rights 59 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 60 | copies of the Software, and to permit persons to whom the Software is 61 | furnished to do so, subject to the following conditions: 62 | 63 | The above copyright notice and this permission notice shall be included in all 64 | copies or substantial portions of the Software. 65 | 66 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 67 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 68 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 69 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 70 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 71 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 72 | SOFTWARE. 73 | 74 | ## Like what you see ? 75 | 76 | I'm also blogging at https://www.dev-metal.com, also find my others 77 | repos here: https://github.com/panique ! -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! 5 | VAGRANTFILE_API_VERSION = "2" 6 | 7 | Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 8 | 9 | # Every Vagrant virtual environment requires a box to build off of. 10 | config.vm.box = "ubuntu/trusty64" 11 | 12 | # Create a private network, which allows host-only access to the machine using a specific IP. 13 | config.vm.network "private_network", ip: "192.168.33.44" 14 | 15 | # Share an additional folder to the guest VM. The first argument is the path on the host to the actual folder. 16 | # The second argument is the path on the guest to mount the folder. 17 | config.vm.synced_folder "./", "/var/www/html" 18 | 19 | # Define the bootstrap file: A (shell) script that runs after first setup of your box (= provisioning) 20 | config.vm.provision :shell, path: "bootstrap.sh" 21 | 22 | end 23 | -------------------------------------------------------------------------------- /application/.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteCond %{REQUEST_FILENAME} !-d 4 | RewriteRule ^ index.php [QSA,L] -------------------------------------------------------------------------------- /application/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "slim/slim": "^3.0", 4 | "tuupola/slim-basic-auth": "^2.0" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /application/index.php: -------------------------------------------------------------------------------- 1 | add(new \Slim\Middleware\HttpBasicAuthentication(array( 11 | // everything inside this root path uses the authentication 12 | "path" => "/api", 13 | // deactivate HTTPS usage (for simplicity) 14 | "secure" => false, 15 | // users (name and password), credentials will be passed via request header, see the client.html for more info 16 | "users" => [ 17 | "demouser" => "123", 18 | ], 19 | "error" => function ($request, $response, $arguments) { 20 | // return the 401 "unauthorized" status code when auth error occurs 21 | return $response->withStatus(401); 22 | } 23 | ))); 24 | 25 | // grouping the /api route, see Slim's group() method documentation for more 26 | $app->group('/api', function () use ($app) { 27 | 28 | $dataForApi = ['yo', 777]; 29 | 30 | // api route "test" which just gives back some demo data 31 | $app->get('/test', function ($request, $response, $args) use ($dataForApi) { 32 | return $response->withJson([ 33 | 'demoText' => $dataForApi[0], // "yo" 34 | 'demoNumbers' => $dataForApi[1] // "777" 35 | ]); 36 | }); 37 | }); 38 | 39 | $app->run(); 40 | -------------------------------------------------------------------------------- /bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Use single quotes instead of double quotes to make it work with special-characters 4 | PROJECTFOLDER='application' 5 | 6 | sudo apt-get update 7 | sudo apt-get -y upgrade 8 | 9 | sudo apt-get install -y apache2 10 | sudo apt-get install -y php5 11 | 12 | # Create project folder, written in 3 single mkdir-statements to make sure this runs everywhere without problems 13 | sudo mkdir "/var/www" 14 | sudo mkdir "/var/www/html" 15 | sudo mkdir "/var/www/html/${PROJECTFOLDER}" 16 | 17 | # setup hosts file 18 | VHOST=$(cat < 20 | DocumentRoot "/var/www/html/${PROJECTFOLDER}" 21 | 22 | AllowOverride All 23 | Require all granted 24 | 25 | 26 | EOF 27 | ) 28 | echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf 29 | 30 | # enable mod_rewrite 31 | sudo a2enmod rewrite 32 | 33 | # restart apache 34 | service apache2 restart 35 | 36 | # remove default apache index.html 37 | sudo rm "/var/www/html/index.html" 38 | 39 | # install git 40 | sudo apt-get -y install git 41 | 42 | # install Composer 43 | curl -s https://getcomposer.org/installer | php 44 | mv composer.phar /usr/local/bin/composer 45 | 46 | cd /var/www/html/${PROJECTFOLDER} 47 | composer install 48 | 49 | # final feedback 50 | echo "Voila!" 51 | --------------------------------------------------------------------------------