PHP Process Indeed.com Job Listing Interface Example

How to Process Indeed.com Job Listing Interface with PHP

PHP Process Indeed.com Job Listing Interface ExampleI’ve worked on a few job recruiting and job listing sites and a few of those sites have requested the ability to show Indeed.com job listings.

The following example is a stand-alone page that will prompt the user for various search criteria then present matching Indeed.com job listing results.

At the bottom, I’ve included some paging in the event the job listing results are more than will fill a page. In order to maintain our search criteria, I’ve included the search criteria in the paging links. This way we can obtain the search criteria from the search form when posted or from the paging links when they are clicked.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Indeed interface</title>
</head>

<body>
<?php

// reference: https://ads.indeed.com/jobroll/xmlfeed

// initialize

	$keywords = "";
	$location = "";		// can be a zip code
	$indeed_start = 1;
	$indeed_limit = 25;
	$indeed_sort = "relevance";
	$indeed_type = "all";
	$indeed_age = "31";
	$indeed_radius = "25";
	$indeed_sort = "";

// post/get variables

	if (isset($_REQUEST["keywords"])) { $keywords = strip_tags($_REQUEST["keywords"]); }
	if (isset($_REQUEST["location"])) { $location = strip_tags($_REQUEST["location"]); }
	if (isset($_REQUEST["start"])) { $indeed_start = strip_tags($_REQUEST["start"]); }
	if (isset($_REQUEST["limit"])) { $indeed_limit = strip_tags($_REQUEST["limit"]); }
	if (isset($_REQUEST["sort"])) { $indeed_sort = strip_tags($_REQUEST["sort"]); }
	if (isset($_REQUEST["type"])) { $indeed_type = strip_tags($_REQUEST["type"]); }
	if (isset($_REQUEST["age"])) { $indeed_age = strip_tags($_REQUEST["age"]); }
	if (isset($_REQUEST["radius"])) { $indeed_radius = strip_tags($_REQUEST["radius"]); }
	if (isset($_REQUEST["sort"])) { $indeed_sort = strip_tags($_REQUEST["sort"]); }

// search form

	echo "<form name='indeedSearch' id='indeedSearch' method='post' action='" . $_SERVER["PHP_SELF"] . "'>\n";
	echo "	<table>\n";
	echo "		<tr>\n";
	echo "			<td>Keywords</td>\n";
	echo "			<td><input type='text' name='keywords' id='keywords' value='$keywords' len='100' maxlength='200' /></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Location (city or zip code)</td>\n";
	echo "			<td><input type='text' name='location' id='location' value='$location' len='100' maxlength='200' /></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Radius</td>\n";
	echo "			<td>\n";
	echo "				<select name='radius'>\n";
	$selected = ($indeed_radius == 5) ? "selected" : "";
	echo "					<option value='5' $selected>Within 5 miles</option>\n";
	$selected = ($indeed_radius == 10) ? "selected" : "";
	echo "					<option value='10' $selected>Within 10 miles</option>\n";
	$selected = ($indeed_radius == 15) ? "selected" : "";
	echo "					<option value='15' $selected>Within 15 miles</option>\n";
	$selected = ($indeed_radius == 25) ? "selected" : "";
	echo "					<option value='25' $selected>Within 25 miles</option>\n";
	$selected = ($indeed_radius == 50) ? "selected" : "";
	echo "					<option value='50' $selected>Within 50 miles</option>\n";
	$selected = ($indeed_radius == 100) ? "selected" : "";
	echo "					<option value='100' $selected>Within 100 miles</option>\n";
	echo "				</select>\n";
	echo "			</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Job Type</td>\n";
	echo "			<td>\n";
	echo "				<select id='type' name='type'>\n";
	$selected = ($indeed_type == 'all') ? "selected" : "";
	echo "					<option value='all' $selected>All job types</option>\n";
	$selected = ($indeed_type == 'fulltime') ? "selected" : "";
	echo "					<option value='fulltime' $selected>Full-time</option>\n";
	$selected = ($indeed_type == 'parttime') ? "selected" : "";
	echo "					<option value='parttime' $selected>Part-time</option>\n";
	$selected = ($indeed_type == 'contract') ? "selected" : "";
	echo "					<option value='contract' $selected>Contract</option>\n";
	$selected = ($indeed_type == 'internship') ? "selected" : "";
	echo "					<option value='internship' $selected>Internship</option>\n";
	$selected = ($indeed_type == 'commission') ? "selected" : "";
	echo "					<option value='commission' $selected>Commission</option>\n";
	$selected = ($indeed_type == 'temporary') ? "selected" : "";
	echo "					<option value='temporary' $selected>Temporary</option>\n";
	echo "				</select>\n";
	echo "			</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Age (Jobs published)</td>\n";
	echo "			<td>\n";
	echo "				<select id='fromage' name='fromage'>\n";
	$selected = ($indeed_age == 'any') ? "selected" : "";
	echo "					<option value='any' $selected>anytime</option>\n";
	$selected = ($indeed_age == '15') ? "selected" : "";
	echo "					<option value='15' $selected>within 15 days</option>\n";
	$selected = ($indeed_age == '7') ? "selected" : "";
	echo "					<option value='7' $selected>within 7 days</option>\n";
	$selected = ($indeed_age == '3') ? "selected" : "";
	echo "					<option value='3' $selected>within 3 days</option>\n";
	$selected = ($indeed_age == '1') ? "selected" : "";
	echo "					<option value='1' $selected>since yesterday</option>\n";
	$selected = ($indeed_age == 'last') ? "selected" : "";
	echo "					<option value='last' $selected>since my last visit</option>\n";
	echo "				</select>\n";
	echo "			</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Sort by</td>\n";
	echo "			<td>\n";
	echo "				<select id='sort' name='sort'>\n";
	$selected = ($indeed_sort == '') ? "selected" : "";
	echo "					<option selected value=''>relevance</option>\n";
	$selected = ($indeed_sort == 'date') ? "selected" : "";
	echo "					<option  value='date'>date</option>\n";
	echo "				</select>\n";
	echo "			</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td>Results per page</td>\n";
	echo "			<td>\n";
	$checked = ($indeed_limit == 10) ? "checked" : "";
	echo "				<input type='radio' name='limit' value='10' $checked> 10\n";
	$checked = ($indeed_limit == 15) ? "checked" : "";
	echo "				<input type='radio' name='limit' value='15' $checked> 15\n";
	$checked = ($indeed_limit == 20) ? "checked" : "";
	echo "				<input type='radio' name='limit' value='20' $checked> 20\n";
	$checked = ($indeed_limit == 25) ? "checked" : "";
	echo "				<input type='radio' name='limit' value='25' $checked> 25\n";
	echo "			</td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td></td>\n";
	echo "			<td><input type='submit' name='indeedSubmit' id='indeedSubmit' value='Search' /></td>\n";
	echo "		</tr>\n";
	echo "	</table>\n";
	echo "</form>\n";

// form posted or paging - show results

	if (isset($_REQUEST['keywords']))
	{

	// parse the search values

		$indeed_keyword_array = array();
		$indeed_keyword_array = explode(" ", $keywords);
		$indeed_keywords = implode(",", $indeed_keyword_array);
		$user_ip = $_SERVER['REMOTE_ADDR'];
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
	
	// counters
	
		$indeed_results = 0;
		$indeed_total = 0;
		$count = 0;
	
	// get indeed results
	
		$api_start = ($indeed_start - 1);
	
		$apiCall = "https://api.indeed.com/ads/apisearch";
		$apiCall .= "?publisher=1234567890123456";		// Your publisher ID
		$apiCall .= "&format=xml";
		$apiCall .= "&q=$indeed_keywords";
		$apiCall .= "&l=$location";
		$apiCall .= "&sort=$indeed_sort";
		$apiCall .= "&radius=$indeed_radius";
		$apiCall .= "&st=";
		$apiCall .= "&jt=$indeed_type";
		$apiCall .= "&start=$api_start";
		$apiCall .= "&limit=$indeed_limit";
		$apiCall .= "&fromage=$indeed_age";
		$apiCall .= "&highlight=1";
		$apiCall .= "&filter=";
		$apiCall .= "&latlong=1";
		$apiCall .= "&co=us";
		$apiCall .= "&chnl=";
		$apiCall .= "&userip=$user_ip";
		$apiCall .= "&v=2";
		$apiCall .= "&useragent=$user_agent";
	
		$xml = simplexml_load_file($apiCall);
	
		if (!$xml)	// If there was no XML response, print an error
		{
			echo "<p>No XML response</p>";
			exit();
		} else {
			if($xml->Fault)	// If there was an error in the response, print a warning.
			{
				echo "<p>XML error</p>";
				$xml = false;
				exit();
			}
		}
	
	// display results
	
		if ($xml)
		{
			$indeed_results = count($xml->results->result);
			$indeed_total = $xml->totalresults;

			foreach ($xml->results->result as $result) 
			{
				$count++;
				$jobtitle = $result->jobtitle;
				$company = $result->company;
				$city = $result->city;
				$state = $result->state;
				$country = $result->country;
				$formattedLocation = $result->formattedLocation;
				$source = $result->source;
				$date = $result->date;
				$snippet = $result->snippet;
				$url = $result->url;
				$onmousedown = $result->onmousedown;
				$latitude = $result->latitude;
				$longitude = $result->longitude;
				$jobkey = $result->jobkey;
				$sponsored = $result->sponsored;
				$expired = $result->expired;
				$indeedApply = $result->indeedApply;
				$formattedLocationFull = $result->formattedLocationFull;
				$formattedRelativeTime = $result->formattedRelativeTime;

				echo "<hr>\n";
				echo "<table>\n";
				echo "		<tr><td>Job Title</td><td>$jobtitle</td></tr>\n";
				echo "		<tr><td>Company</td><td>$company</td></tr>\n";
				echo "		<tr><td>City</td><td>$city</td></tr>\n";
				echo "		<tr><td>State</td><td>$state</td></tr>\n";
				echo "		<tr><td>Country</td><td>$country</td></tr>\n";
				echo "		<tr><td>Formatted Location</td><td>$formattedLocation</td></tr>\n";
				echo "		<tr><td>Source</td><td>$source</td></tr>\n";
				echo "		<tr><td>Date</td><td>$date</td></tr>\n";
				echo "		<tr><td>Snippet</td><td>$snippet</td></tr>\n";
				echo "		<tr><td>URL</td><td>$url</td></tr>\n";
				echo "		<tr><td>On Mouse Down</td><td>$onmousedown</td></tr>\n";
				echo "		<tr><td>Latitude</td><td>$latitude</td></tr>\n";
				echo "		<tr><td>Longitude</td><td>$longitude</td></tr>\n";
				echo "		<tr><td>Job Key</td><td>$jobkey</td></tr>\n";
				echo "		<tr><td>Sponsored</td><td>$sponsored</td></tr>\n";
				echo "		<tr><td>Expired</td><td>$expired</td></tr>\n";
				echo "		<tr><td>Indeed Apply</td><td>$indeedApply</td></tr>\n";
				echo "		<tr><td>Formatted Location Full</td><td>$formattedLocationFull</td></tr>\n";
				echo "		<tr><td>Formatted Relative Time</td><td>$formattedRelativeTime</td></tr>\n";
				echo "	</table>\n";
			}
		}

	// paging
	
		if ($indeed_total > 0)
		{
			$indeed_next = $indeed_start + $indeed_limit;
			$indeed_prev = $indeed_start - $indeed_limit;
	
			echo "	<table>\n";
			echo "		<tr>\n";
			
			if ($indeed_start > 1) 
			{
				echo "<td><a href='indeed.php?keywords=$keywords&location=$location&start=1&limit=$indeed_limit&sort=$indeed_sort&type=$indeed_type&age=$indeed_age&radius=$indeed_radius'>First</a></td>";
				echo "<td><a href='indeed.php?keywords=$keywords&location=$location&start=$indeed_prev&limit=$indeed_limit&sort=$indeed_sort&type=$indeed_type&age=$indeed_age&radius=$indeed_radius'>Previous</a></td>";
			}
			
			$page_current = ((($indeed_start - 1 ) + $indeed_limit) / $indeed_limit); 
			$page_count = ceil($indeed_total / $indeed_limit);
			$page_start = 1;
			$page_stop = $page_count;		
			
			if ($page_count > 5) {
				if (($page_current - 2) > 0 && ($page_count - $page_current) > 2) 
				{
					$page_start = $page_current - 2;
				} else {
					$page_start = (($page_current - 2) <= 0 ? 1 : $page_count - 4);
				}
				$page_stop = (($page_start + 4) < $page_count ? $page_start + 4 : $page_count);
			}
	
			for ($i=$page_start; $i<=$page_stop; $i++)
			{
				$new_page_start = ((($i * $indeed_limit) + 1) -  $indeed_limit);
	
				if ($new_page_start <= $indeed_total)
				{
					if ($i == $page_current)
					{
						echo "<td><a href='indeed.php?keywords=$keywords&location=$location&start=$new_page_start&limit=$indeed_limit&sort=$indeed_sort&type=$indeed_type&age=$indeed_age&radius=$indeed_radius'><b><u> $i </u></b></a></td>\n";
					} else {
						echo "<td><a href='indeed.php?keywords=$keywords&location=$location&start=$new_page_start&limit=$indeed_limit&sort=$indeed_sort&type=$indeed_type&age=$indeed_age&radius=$indeed_radius'>$i</a></td>\n";
					}
				}
			}
	
			if ($indeed_next < $indeed_total) 
			{
				echo "<td><a href='indeed.php?keywords=$keywords&location=$location&start=$indeed_next&limit=$indeed_limit&sort=$indeed_sort&type=$indeed_type&age=$indeed_age&radius=$indeed_radius'>Next</a></td>";
			}
	
			echo "		</tr>\n";
			echo "	</table>\n";
		}
	} // form posted

?>
</body>
</html>

Copy Code

References

Indeed XML Feed
PHP $_REQUEST
PHP strip_tags
PHP isset
PHP echo
PHP arrays
PHP explode
PHP implode
PHP $_SERVER
PHP simplexml_load_file

Leave a Reply

I appreciate and welcome your comments. Please keep in mind that comments are moderated according to my comment policy.
Your email address will not be published. Required fields are marked *