Tuesday, March 17, 2015

Integrating Wordpress with a Jolie service

Wordpress developers could have the problem to integrate the website they are developing with some extra data which come from external resources. Data synchronization could be not easy, it must be scheduled, data are visible on the web application with a period equal to the synchronization timeout and it usually requires a level of maintenance that is high with respect to the amount of synchronized data.

In these cases it could be useful to synchronously call an external server in order to retrieve the data we need. Jolie could be very helpful for developing a fast API server to be integrated within your web site.

This short post describes a way for easily integrate Wordpress with a Jolie service.

First of all, let me consider the simple case of getting a list of addresses from a Jolie service. This service could be written as it follows:

type GetAddressRequest: void {
    .name: string
    .surname: string
}

type GetAddressResponse: void {
    .address: string
}

interface ServerInterface {
    RequestResponse:
        getAddress( GetAddressRequest )( GetAddressResponse )
}


execution{ concurrent }

inputPort AddressService {
    Location: "socket://localhost:9001"
    Protocol: http
    Interfaces: ServerInterface
}

init {
    with( person[ 0 ] ) {
        .name = "Walter";
        .surname = "White";
        .address = "Piermont Dr NE, Albuquerque (USA)"
    };
    with( person[ 1 ] ) {
        .name = "Homer";
        .surname = "Simpsons";
        .address = "Street 69, Springfield USA"
    };
    with( person[ 2 ] ) {
        .name = "Sherlock";
        .surname = "Holmes";
        .address = "221B Baker Street"
    }


}

main {
    getAddress( request )( response ) {      
        index = 0;
        while( index < #person
                && person[ index ].name != request.name
                && person[ index ].surname != request.domain ) {
            index ++
        };
        if ( index == #person ) {
            throw( PersonNotFound )
        } else {
            response.address = person[ index ].address
        }
    }

}


In this case I simulated a database with a simple vector of persons. The service is located in socket://localhost:9001 and you can easily invoke him through a browser by using this url: http://localhost:9001/getAddress?name=Sherlock&surname=Holmes

Clearly, this is an example which runs on your local machine, it is simple to export it in a distributed scenario by simply running Jolie on the remote machine, let me suppose it is located at IP X.X.X.X.

When the Jolie service is executed, we can invoke it within a wordpress page. In order to do this, simply add the following PHP code on the page where you want to show the results:

$data = array(
    'method' => 'POST',
    'timeout' => 45,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array(),
    'body' => array( 'name' => 'Sherlock', 'surname' => 'Holmes' )
    );

$url = 'http://X.X.X.X:9001/getAddress';
$result = wp_remote_post( $url, $data );

print_r( $result );

if( is_wp_error( $result ) ) {
echo $result->get_error_message();
}


Where $result contains the response message.

You can also exploit a JSON format message by adding the following parameter to the http protocol of the Jolie service and re-arranging the PHP code for managing JSON messages.

 inputPort AddressService {
    Location: "socket://localhost:9001"
    Protocol: http { .format="json" }
    Interfaces: ServerInterface
}


Moreover you could connect the Jolie service to a database for retrieving the data you need, check the Jolie tutorial page for getting how to use databases in Jolie.