Skip to main content

Home  »  Business NewsSpotlight   »   WhatJobs.com Tips – How to build your own job search engine

WhatJobs.com Tips – How to build your own job search engine

How to build a job search engine

A while ago, a friend of mine asked me if I can help him on a small PHP project. The project was about turning his static website into a place that people can search for jobs. He was looking for a solution to let his visitors search for job content and see the results within the pages of his website.

I thought it would take a long time to implement such a system because it was about storing the jobs in a database, then showing them to the users based on their search criteria. With this in mind, I prepared myself for a project which I needed to be involved with for a long while.

He asked me to look at a third-party website offering an API letting people have their own job search engine. They claimed there would be no hassle in dealing with their jobs, configuring a database, or any other technical issues. The API was letting websites access the latest vacancies and show them to their visitors. In return, the website that was serving these jobs to the visitors could make money every time a user clicked on any job search results.

After reading and playing with the sample codes, I found no need to search for the user's keyword in the local database. Moreover, I did not need to have any database to store the jobs at all! Actually, the only thing that I had to do was get the user’s request and send it to the API. After receiving data from API, I was able to show them to the user.

I know it sounds crazy to have a job search engine without having any database, but these days you see even stranger things! The project was something cool, so I decided to accept the challenge and see the outcome!

The first thing I did was preparing an API URL to see the result. There were two options: first, returning the result in XML format and the second, returning the same result in JSON format.

I opened the URL in my browser and set the search keyword to sales:

https://whatjobs.com/api/v1/jobs.xml?publisher=1&keyword=sales

Also tried the JSON URL and changed the keyword and set a location:

https://whatjobs.com/api/v1/jobs.json?publisher=1&keyword=manager&location=london

It could not be easier than this. I had the search result, and the only other thing I had to do was format them with the look and feel of my friend's website and then show them to the user!

But I was too excited to realise that before showing any result to the users, I should have asked them what they wanted to search for! So I decided to start with a straightforward page with a search button for two input boxes: One for keyword and the other for location.

<form>

    <input name="keyword">

    <input name="location">

    <input type="submit" value="Search">

</form>

As such, when a user hit the search button I would receive the user's keyword and location in the $_GET variable, and by having these two values, I was able to create the URL to call the API. According to the documentation I should have sent additional information like publisher ID, user IP and user agent otherwise, the tracking system wouldn't work, and my friend wouldn't make any money ;-)

The final code to generate the API URL was:

$data = array(

    'publisher' => 1,

    'user_ip' => $_SERVER['REMOTE_ADDR'],

    'user_agent' => $_SERVER['HTTP_USER_AGENT'],

    'keyword' => $_GET['keyword'],

    'location' => $_GET['location'],

);

$api_url = "https://whatjobs.com/api/v1/jobs.json?" . http_build_query($data);

The rest was easy, calling the API and converting JSON to an array and storing the result in the $result variable:

$result = json_decode(file_get_contents($api_url), true);

Now I had everything I was looking for, and I would format the result and show them to the user. It was only matter of the colours and the styles that could make the result suitable for the user. To make my life easy and create a responsive page for mobile users, I used Bootstrap to show the result to the user by including Bootstrap CSS file and adding some classes to the tags. Then I used a table to show each job in a row.

<table class="table table-condensed">

<?php foreach ($result['data'] as $job): ?>

    <tr>

        <td>

            <div class="col-sm-6 h2">

                <a href="<?php echo $job['url']; ?>" target="_NEW" rel="nofollow"><?php echo $job['title']; ?></a>

            </div>

            <br>

            <?php if (! empty($job['salary'])): ?>

            <div class="col-sm-6 text-right" style="color: #888;"><strong><?php echo $job['salary']; ?></strong></div>

            <?php endif; ?>

            <?php if (! empty($job['location'])): ?>

            <div class="col-sm-6 text-right" style="color: #888;">Location: <strong><?php echo $job['location']; ?></strong></div>

            <?php endif; ?>

            <div class="col-sm-10"><?php echo $job['snippet']; ?></div>

            <div class="col-sm-2">

                <?php if (! empty($job['logo'])): ?>

                <a href="?<?php echo generateURL(array('keyword' => $job['company'])); ?>"><img align="right" src="<?php echo $job['logo']; ?>"></a>

                <?php endif; ?>

            </div>

            <div class="col-sm-12" style="color: #999;">

                <?php if (! empty($job['company'])): ?>

                Company: <strong><?php echo $job['company']; ?></strong>

                <?php endif; ?>

                <?php if (! empty($job['job_type'])): ?>

                Job type: <strong><?php echo $job['job_type']; ?></strong>

                <?php endif; ?>

                (<span><?php echo $job['age']; ?></span>)

            </div>

        </td>

    </tr>

<?php endforeach; ?>

</table>

Also, according to the API tracking document I had to place the tracking code near the results. It was a script tag to include the JavaScript tracking code. I placed it after the table tag.

Everything was almost ready, and I could finish this project very quickly by putting the pagination at the bottom of the page. Building the pagination was easy enough because there were some other values in the API result to show the current page and the last page. By having these values and doing some calculation, I managed to create the pagination index. So on each API call, I was able to send the page number to receive the related result.

function getPaginationIndex($current_page, $last_page, $index_count)

{

    $low_index = $current_page - round($index_count / 2) + 1;

    $high_index = max($low_index, 1) + $index_count - 1;

    if ($high_index > $last_page)

    {

        $high_index = $last_page;

        $low_index = $high_index - $index_count + 1;

    }

    $low_index = max($low_index, 1);

    return array($low_index, $high_index);

}

I was done and my coffee was still on the table, so I still had time to add some other options to make it look like a professional website. I decided to add a list of categories and locations to the home page. That was a very useful option for the users so they could just click and browse jobs relevant to them. The only challenge was building the lists, then I could create a URL for each of these items.

function getLocations()

{

    return array(

        'Basingstoke', 'Exeter', 'Nottingham', 'Birmingham', 'Gloucester', 'Oxford', 'Bradford', 'Guildford', 'Oxfordshire',

        'Brighton', 'Hampshire', 'Peterborough', 'Bristol', 'Hertfordshire', 'Preston', 'Buckinghamshire', 'Kent', 'Reading',

        'Cambridge', 'Leeds', 'Sheffield', 'Cambridgeshire', 'Leicester', 'Slough', 'Chelmsford', 'Leicestershire',

        'Southampton', 'Cheshire', 'Liverpool', 'Stockport', 'Chester', 'London', 'Surrey', 'Coventry', 'Maidstone', 'Swindon',

        'Croydon', 'Manchester', 'Wakefield', 'Derby', 'Milton Keynes', 'Warrington', 'Derbyshire', 'Newcastle upon Tyne',

        'West Yorkshire', 'Doncaster', 'Northampton', 'York',

    );

}

function getCategories()

{

    return array(

        'Accounting', 'Finance', 'Pharmaceutical', 'Administrative', 'Food Service', 'PR', 'Advertising', 'Healthcare',

        'Publishing', 'Agriculture', 'Hospitality', 'Real Estate', 'Architecture', 'Human Resources', 'Restaurant', 'Arts',

        'Insurance', 'Retail', 'Banking', 'IT', 'Sales', 'Computer', 'Law Enforcement', 'Scientific', 'Construction', 'Legal',

        'Security', 'Consulting', 'Loans', 'Social Care', 'Customer Service', 'Logistics', 'Telecommunications', 'Education',

        'Management', 'Training', 'Energy', 'Manufacturing', 'Transportation', 'Engineering', 'Marketing', 'Travel', 'Facilities',

        'Mechanical', 'Volunteering',

    );

}

When I placed all the pieces together, I was really pleased with the final result! It was the first time that I had finished a project so quickly.