"The human factor is truly security's weakest link" Kevin D. Mitnick

Using the Job Queue API of the Zend Platform

Posted: April 25th, 2009 | Author: | Filed under: Zend | Tags: , , , , | 6 Comments »

One of the features of the Zend Platform is the Job Queue, a server application that is able to schedule and manage the execution of php scripts (jobs).

The Job Queue can be used to create asyncronous execution of php script and provide, for instance, the scalability of a server application (for more info read the White Paper “Scalability and Responsiveness with Zend Platform’s Job Queue” by Dotan Perry and Shie Erlich).

You can manage the Job Queue feature of the Zend Platform with a web interface (see the figure below) or directly in PHP with an API system.

job queue

Using the web interface you can schedule the execution of a job (a php script) that is located into the JobQueue directory of the Zend Platform (the default is /usr/local/Zend/Platform/JobQueue in a Linux environment). You can also suspend and resume jobs from the queue, view the statistic of the job queue, view the history of each job execution and setting the parameter of the queue (maximal queue depth, maximal re-queue times, queue alias, scripts folder, etc).

As I said you can use the job queue also from PHP with the use of a API system. Basically the Job Queue Zend API contains two main classes to manage the connection to a queue server and to schedule the execution of jobs. This classes are ZendAPI_Queue and ZendAPI_Job. Is very easy to use this API and you can schedule the execution of your php script in a few lines of code. Let me show this introducing some examples.

In order to connect to a queue server you have to specify the host:port address and the password of the queue server (the default port of the queue server is 10003). Here there is an example:

1
2
3
4
$queue= new ZendAPI_Queue('192.168.1.10:10003');
if ((empty($queue)) || (!$queue->login('password'))) {
    throw new Exception('Error on queue connection');
}

When you have established a connection you can manage the scheduling of a job. For instance here we scheduled the execution of a job named send_email.php for the next 4 hours.

1
2
3
$job = new ZendApi_Job("send_email.php");
$job->setScheduledTime(time() + 4*3600);
$id = $queue->addJob($job);

If you want to search for a job into a queue server you can use the getJobsInQueue method of the ZendAPI_Queue class.
For instance you can remove the failed jobs from a queue server with the following code:

1
2
3
4
5
6
7
8
9
10
$queue= new ZendAPI_Queue('192.168.1.10:10003');
if ((empty($queue)) || (!$queue->login('password'))) {
    throw new Exception('Error on queue connection');
}
$jobs_status = array(status=>JOB_QUEUE_STATUS_LOGICALLY_FAILED);
$all_failed_jobs = $queue->getJobsInQueue($jobs_status);
foreach ($all_failed_jobs as $failed_job) {
      echo "removing job $failed_job from queue...";
      $queue->removeJob($failed_job);
}

In this post i introduced only the main functions of the Job Queue Zend API, for the complete list you can read the User Guide of Zend Platform 3.6 at pag. 295-305.
In my opinion using the Job Queue feature of the Zend Platform you can explore a new way to think about web solutions with asyncronous execution of PHP code.


Display geographic data with PHP and Google Map

Posted: April 11th, 2009 | Author: | Filed under: PHP | Tags: , , , , , | 5 Comments »

Today i created an example in PHP to display data into a Google map. I used my GMaps class (see my previous post) to retrieve geographic information for all the provinces of Italy, as you know i’m italian. I inserted all the data into a table of MySQL with the following fields: id, province, code, area, latitude, and longitude where code is the italian automobile code and area is the name of the region. For instance the province of Pescara has the following record: 69,’Pescara’,'PE’, ‘Abruzzi’, 42.4648, 14.2141.

Provinces of Italy



The example in PHP uses this database to display the geographic data of all the provinces of Italy. You can select the geographic area (region) in a combobox and display the data on a Google Map.

You can download the source code of the example with the database here.

Click here to see the online example.


How to use the Google Maps API with PHP

Posted: April 8th, 2009 | Author: | Filed under: PHP | Tags: , , , , , , , | 34 Comments »

The Google Maps API are a very useful system to get geographic information. Maybe you are thinking cool! but i’m not a GIS developer so why i need to continue to read this post? Let me do an example. Scenario: you have to check the validity of the geo data of a list of customers stored into a database. Using the Google Maps API you can easly compare the data stored into your database with the google data and fix the errors.
In this post i propose a PHP class that uses the Google Maps API to provide geographic information from a generic address (city, country,street+city, etc). This class is named GMaps and in the follow there is a use case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require_once 'GMaps.php';
 
// Your google key
$google_key = '';
 
if (!empty($_POST)) {
  $search= strip_tags($_POST['search']);
}    
echo '<form action="example.php" method="post">';
echo '<input name="search" type="text" />';
echo '<input type="submit" value="Get geographic data!" />';
echo '</form>';
 
if (!empty($search)) {
  // Get the Google Maps Object
  $GMap = new GMaps($google_key);
  if ($GMap->getInfoLocation($search)) {
    echo 'Address: '.$GMap->getAddress().'<br>';
    echo 'Country name: '.$GMap->getCountryName().'<br>';
    echo 'Country name code: '.$GMap->getCountryNameCode().'<br>';
    echo 'Administrative area name: '.$GMap->getAdministrativeAreaName().'<br>';
    echo 'Postal code: '.$GMap->getPostalCode().'<br>';
    echo 'Latitude: '.$GMap->getLatitude().'<br>';
    echo 'Longitude: '.$GMap->getLongitude().'<br>';
  } else {
    echo "The response of Google Maps is empty";
  }
}

To use the GMaps class we initialize it with a valid Google key (line 16) and we get the geographic information with the method getInfoLocation (line 17), that’s it!
Here you can download the class GMaps.php with this example (GMaps.zip).
In order to execute the class you must have a valid Google API key. If you don’t have this key you can generate a new one here (every key is related to a specific url of your application).