├── README.md ├── Vagrantfile ├── application ├── composer.json └── index.php └── bootstrap.sh /README.md: -------------------------------------------------------------------------------- 1 | # simple-php-api-client 2 | 3 | An extremely simple API client (with authentication) example, 4 | written in PHP on top of [Guzzle](https://github.com/guzzle/guzzle) 5 | (a HTTP client for PHP). This repo shows the "client", meaning the part 6 | that get data from an API, there's another repo 7 | (https://github.com/panique/simple-php-api) showing a "server", meaning 8 | the part that DELIVERS the data. 9 | 10 | ## What's inside ? 11 | 12 | It's just a simple PHP file that makes an example API call, 13 | authenticates and then echos out the JSON response. This file uses 14 | the excellent Guzzle HTTP client (that makes it easy to perform HTTP(S) 15 | requests in PHP with a single line of code) which will be installed 16 | via Composer (I think you probably know how Composer works as writing 17 | APIs is not really a beginner task :)). 18 | 19 | ## How to install 20 | 21 | For manual install, simply put composer.json and index.php somewhere on 22 | your server, install the Composer dependencies and run index.php! 23 | 24 | For automatic installation (which will install a Ubuntu 14.04 LTS box 25 | including Apache, PHP etc. and perform Composer install via Vagrant) 26 | simply do a "vagrant up" in the folder where *Vagrantfile* is. 27 | Your application is then reachable under 192.168.33.45. 28 | 29 | Please note that the application by default tries to get data from 30 | 192.168.33.**44** which is part of the API server repo under 31 | (https://github.com/panique/simple-php-api). 32 | -------------------------------------------------------------------------------- /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.45" 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/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "guzzlehttp/guzzle": "^6.0" 4 | } 5 | } -------------------------------------------------------------------------------- /application/index.php: -------------------------------------------------------------------------------- 1 | request('GET', 'http://192.168.33.44/api/test', [ 11 | // username and password 12 | 'auth' => ['demouser', '123'] 13 | ]); 14 | 15 | // simple API data handling, echo out something from result 16 | if ($res->getStatusCode() == 200) { 17 | // we expect the result to be JSON here, for simplicity no further checks 18 | $jsonResult = json_decode($res->getBody()); 19 | // demoText should be "yo" 20 | echo $jsonResult->demoText; 21 | } 22 | -------------------------------------------------------------------------------- /bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Use single quotes instead of double quotes to make it work with special-character passwords 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 | # install dependencies via Composer 47 | cd /var/www/html/${PROJECTFOLDER} 48 | composer install 49 | 50 | # final feedback 51 | echo "Voila!" 52 | --------------------------------------------------------------------------------