Monthly Archives: June 2015

REST API in PHP (on Apache)

Let’s suppose you find yourself in a situation where you have to use PHP (because you’ve got an Apache server you have to work with). Coming from a tomcat and nodejs backend, what’s the closest thing one can do to make this a more familiar and easier to work with architecture?

I introduce the Slim Framework. It’s very easy to use, and if you are familiar with nodejs and express, you’ll find this familiar.

Slim suggests you install them using Composer. Being more of a programmer than IT guy, I don’t like to have to install an util to install another util. That makes my code less portable because if I move it, I’ll have to remember to tell the IT guy what dependencies to include for me. I like my code to be drop-in-ready.

Instead, I suggest you just download their latest release, unzip, and copy their Slim folder and index.php into your webapp.

I also suggest you create a folder called /rest in your webapp root. So your folder should look something like this

/htdocs
  /rest
    /index.php
    /Slim
      /Slim.php
      /Router.php
      ...

SUPER IMPORTANT STEP!!!!!
They won’t highlight this because it’s Apache-specific, but now create a .htaccess file in the /rest folder.

The contents of .htaccess should be

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Don’t worry too much about what this means. It’s basically rewriting the url when clients request the index.php file so that it can translate them to REST paths.

So that’s it!

On yeah, the code.

Here’s a sample

<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app->contentType("application/json");

$app->get('/resource/:name','getResource');

function getResource($name) {
  $resource = array(
    'name' => $name
    );
  echo json_encode($resource);
}

$app->run();
?>

Let’s go over some pieces…

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

This is just some required code to initialize the framework. This is specific to the download-unzip method of installing Slim. (There’s a different way of initializing if you installed using Composer.)

$app = new \Slim\Slim();
$app->contentType("application/json");

The first line creates an instance of Slim.
The next line is optional, which sets ALL responses to be of type json.
You could also include it just within each routing function to specify the content type for just those routes.

$app->run();

This starts the service..

$app->get('/resource/:name','getResource');

function getResource($name) {
  $resource = array(
    'name' => $name
    );
  echo json_encode($resource);
}

This is really the meat of the REST app. This should look very familiar to you if you’ve used node/express before.

You just write $app->get (or $app->post, $app->delete…). The first argument is the path. Notice that :name refers to a variable whose value is passed in as a parameter to the function. The following argument(s) are callables or functions. I can also give it a name of a function as I have done here.
I say the “following argument(s)” because you can pass in more than one. You can chain a series of callables, allowing you to do things like authentication, or header rewriting, …

There’s a lot that I’m not showing you but it’s fairly easy to learn once you got it up and running. So go learn it!

(I assume you know the client-side way of calling these functions, if not, browse around this blog or the internet for great tutorials on how to make REST calls using ajax, angular, or one of a million other methods)

Tagged , , ,

Point all Domains to one server (in Apache)

Let’s say the marketing department can’t make up its mind on which domain name to use. It’s between ultimatefoo.com or uberfoo.com.

So you build a website and point both domains to the same server. Easy.

Then one day, they say, hey, let’s go with uberfoo.com cuz it’s shorter and “uber” is the buzz word for the moment. Now they want you to redirect customers who go to ultimatefoo.com to uberfoo.com just so it’s consistent.

On the domain management side, you can just forward all request to ultimatefoo.com to uberfoo.com.
In GoDaddy, you select your domain and go to the Settings tab to find the Forwarding section
Screen Shot 2015-06-05 at 4.32.54 PM

 

Then Click Add Forwarding

Screen Shot 2015-06-05 at 4.32.42 PM

Fill in the Forward To section:

Screen Shot 2015-06-05 at 4.32.06 PM

 

Simple enough… if you have IT access to the domain. But what if you didn’t?

Suppose you wanted to do it strictly from Apache. Here’s how.

Open the .conf file. Its location is machine-dependent. I found mine at /etc/apache2/sites-available/25-http.conf

Now you’ll need to modify it to create two <VirtualHost> sections. One should already be there.

For the existing one, modify the ServerName if you haven’t done so already to uberfoo.com:

<VirtualHost *:80>
  ServerName uberfoo.com
  DocumentRoot "/var/www"
  ...
</VirtualHost>

Next create a second VirtualHost that redirects to it like so

<VirtualHost *:80>
  ServerName ultimatefoo.com
  Redirect "/" "http://uberfoo.com"
</VirtualHost>

Now you don’t even have to bother the IT guy in charge of domains 🙂

Tagged ,