Basic Python RESTful Server bridging PHP with Python

I run into an issue recently, I got a use case to connect to the Luminoso service using their API, Luminoso have an Authentication library in Python, my backend server is written in PHP.

I had to make an API call from my php backend to include the Luminoso Python authentication package, so the solution I came up with was to code a basic RESTful Server that would receive a call from my PHP backend, when the request was in the Python code, invoke the Luminoso Python Authentication library to sign an API request to the Luminoso server.

You can get my code from GitHub here Basic-Python RESTful Server


Flask micro framework –
Luminoso python client –
Tested with Python 2.7

If you running CentOS 6.3, read this article Installing python 2.7 on centos 6.3

How to setup
During development I setup a virtual environment, here is how I did that.

If you are on Mac OS X or Linux, chances are that one of the following two commands will work for you:

$ sudo easy_install virtualenv or even better:

$ sudo pip install virtualenv

Once you have virtualenv installed, just fire up a shell and create your own environment. I created a project folder and a env folder within:

$ mkdir myproject

$ cd myproject

$ virtualenv env

New python executable in env/bin/python

Installing distribute…………done.

Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:

$ . env/bin/activate

If you are a Windows user, the following command is for you:

$ env\scripts\activate

If you are using CentOS 6.3

You have to set your Python interpreter.

$ virtualenv -p /usr/bin/python2.7 env

This will use the Python interpreter in /usr/local/bin/python2.7 if you followed the instructions Installing python 2.7 on centos 6.3

To begin using the virtual environment activate:

$ source venv/bin/activate

The name of the current virtual environment will now appear on the left of the prompt (e.g. (env)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the env folder, isolated from the global Python installation.

Either way, you should now be using your virtualenv (notice how the prompt of your shell has changed to show the active environment).

Want to learn more about virtual environment – you can find it here.

Now you can just enter the following command to get Flask activated in your virtualenv:

$ pip install Flask

A few seconds later and you are good to go.

As I’m going to use the Luminoso Python client, I need to install it

$ pip install luminoso_api

On Centos 6.3 if you have not changed the names in your virtual environment

$ pip2.7 install Flask

$ pip2.7 install luminoso_api

Run the rest-server

$ python &

On Centos 6.3 if you have not changed the names in your virtual environment

$ python2.7 &

You can test it from a browser or a RESTful client

PHP Code

The web application is built on codeigniter, here is how I code the call in PHP to call the basic python RESTful server

public function get_luminoso_score($project_name, $article_text) 
    // Load the rest client spark
    // Load the library

    // Set config options (only 'server' is required to work)
    $config = array('server' => 'http://localhost:5000/');
    // Run some setup


    $param = array('project_name' => $project_name,
                    'article_text' => $article_text,
                    'luminoso_account' => Settings::get('luminoso_account'), 
                    'luminoso_account_name' => Settings::get('luminoso_account_name'),
                    'luminoso_password' => Settings::get('luminoso_password'));

    $json = json_decode($this->rest->get('api/v1.0/get_correlation', $param), true);

    if ($json) 
        return $json;
    else {
        log_message('error', 'Bad response from Luminoso');
        return false;

How to adopt for other python clients

It is easy to adopt for other python clients – just replace the Luminoso python client details with the python client that you need to use.


Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Subscribe for updates

Enter your email address to subscribe to receive notifications of new content by email.

Join 5,882 other subscribers