In the previous articles we worked through what composer is, who uses it, and how to install it. Here we will cover some basic use case examples of how to acquire packages using the composer tool we previously setup.
The example documented in this article can be done either locally, or on your Liquid Web Fully Managed cPanel server, in either case these directions should be run as the user owning the website files. On a cPanel server this would mean you’re running these via SSH logged in as the cPanel user and you would be starting from within public_html.
Example #1: Getting GuzzleHTTP using Composer
One of the most popular PHP HTTP clients, Guzzle is a library that can make sending HTTP requests simple and easy. As a widely used and well-documented library, Guzzle is an easy package for any developer or designer to take advantage of.
To try out Guzzle run the following commands:
$ mkdir guzzleTest
$ cd ./guzzleTest/
$ composer require guzzlehttp/guzzle
Then create an index.php file in the same folder with the following content:
<?php require_once 'vendor/autoload.php'; $client = new GuzzleHttp\Client(); // Make a request $res = $client->request('GET', 'http://www.timeapi.org/utc/now.json'); // Output the status code of the response echo 'Page response code: '.$res->getStatusCode(); echo "<hr/>"; // "200" // Output headers of the response echo 'Response Content-Type header: '; print_r( $res->getHeader('content-type') ); echo "<hr/>"; // 'application/json; charset=utf8' // Output the actual content (body) of the response echo 'Response Body content: '; echo $res->getBody(); echo "<hr/>"; ?>
Opening the new index.php file in your browser should yield a page that shows: the HTTP response code, the ‘Content-Type’ header of the response provided, and the actual content of the response.
Example #2: Getting a framework
While composer is mainly used to get specific libraries and packages needed for a site to run, it’s also possible for a whole framework or CMS to be provided using composer. Laravel is one of many popular PHP frameworks that use composer to distribute their core files. An example of using composer to get Laravel can be done with the following commands:
$ composer create-project --prefer-dist laravel/laravel ./laraTest
Once this command has been executed composer will do a number of things for you; it will create the “laraTest” folder, initialize the composer.json file, get any necessary dependencies and then setup the Laravel-specific files.
To verify this example you will need some familiarity with the Laravel framework, however you can verify that composer did its job by checking the file structure. To check the file structure run the following command:
$ ls -lah
You should see a structure similar to:
drwxr-xr-x 11 user users 4.0K Aug 8 13:17 .
drwxr-xr-x 10 user nginx 4.0K Aug 8 13:16 ..
drwxr-xr-x 10 user users 4.0K Apr 27 09:01 app
-rwxr-xr-x 1 user users 1.7K Apr 27 09:01 artisan
drwxr-xr-x 3 user users 4.0K Apr 27 09:01 bootstrap
-rw-r–r– 1 user users 1.3K Apr 27 09:01 composer.json
-rw-r–r– 1 user users 111K Aug 8 13:17 composer.lock
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 config
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 database
-rw-r–r– 1 user users 458 Aug 8 13:17 .env
-rw-r–r– 1 user users 423 Apr 27 09:01 .env.example
-rw-r–r– 1 user users 61 Apr 27 09:01 .gitattributes
-rw-r–r– 1 user users 73 Apr 27 09:01 .gitignore
-rw-r–r– 1 user users 503 Apr 27 09:01 gulpfile.js
-rw-r–r– 1 user users 212 Apr 27 09:01 package.json
-rw-r–r– 1 user users 1.1K Apr 27 09:01 phpunit.xml
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 public
-rw-r–r– 1 user users 1.9K Apr 27 09:01 readme.md
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 resources
-rw-r–r– 1 user users 567 Apr 27 09:01 server.php
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 storage
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 tests
drwxr-xr-x 29 user users 4.0K Aug 8 13:17 vendor