Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ Indeed API

PHP interface to Indeed job search API.

I will write better documentation when I can but look through the source for
more detail.
Description about queries format you can see there https://ads.indeed.com/jobroll/xmlfeed

But in original documentation you will not find this

- Query for search specific job by him jobkey: ```q=jobkey:c798c4fed37edf59``` or several keys ```q=jobkey:01ba63a5b2d8e3d4,jobkey:f89e5b8175aada87```
- Query by title: ```q=title:Title+of+job``` will returns all jobs matched with this title
- Query by one of any word from list: ```q=php+or+js+or+javascript+or+css```

in this examples the char "+" is encoded "space" char.

Example of Usage
-------------------------------------------------------------------------------
Expand All @@ -15,7 +21,7 @@ Example of Usage
require 'indeed-api.php';

$indeedAPI = new IndeedAPI( 123455667 );
$indeedAPI->setDefaultParams( array(
$indeedAPI->setParams( array(
'co' => 'gb'
) );

Expand All @@ -38,15 +44,21 @@ Author(s)

- Neil Sweeney <neil.sweeney@fubra.com>

Contributor(s)
-------------------------------------------------------------------------------

- AdamasAntares https://github.com/adamasantares


Releases
-------------------------------------------------------------------------------

### 1.0.0 (2013-11-21)
### 1.0.8 (2015-02-07)

Initial release

* Query the job search API
* Pass in default values
* Choose between JSON or XML feed
* Choose to return object or raw document
* Fix useragent and userip for CLI
188 changes: 80 additions & 108 deletions indeed-api.php
Original file line number Diff line number Diff line change
@@ -1,199 +1,171 @@
<?php
/**
* Indeed API Class
* https://github.com/fubralimited/indeed-api
*
* PHP class to interact with the Indeed API
* https://ads.indeed.com/jobroll/
*
* @package indeed-api-class
* @license http://opensource.org/licenses/MIT
* @version 1.0.0
* @version 1.0.8
*/
class IndeedAPI {

/**
* API version we are using
*
* @var integer
*/
private $version = 2;

/**
* Publisher ID for affiliates
*
* @var integer
*/
private $pubID;

/**
* Format which data is returns; either `json` or `xml`
* Default is `json` for ease
*
* URL of API server
* @var string
*/
private $format = 'json';
private $API_URL = 'http://api.indeed.com/ads/apisearch';

/**
* Root URL
*
* @var string
*/
private $rootURL = 'http://api.indeed.com/ads/apisearch';

/**
* URL of API
*
* @var string
* @var string
*/
private $url = '';

/**
* Parameters that the feed will accept; see a list of descriptions at
* https://ads.indeed.com/jobroll/xmlfeed
*
* @var array
*/
private $acceptedParams = array(
'q',
'l',
'sort',
'radius',
'st',
'jt',
'start',
'limit',
'highlight',
'filter',
'latlong',
'co',
'chnl',
);
private $lastQueryURL = '';

/**
* Default parameters for the API
*
* @var array
* @see https://ads.indeed.com/jobroll/xmlfeed
*/
private $defaultParams = array();
private $defaultParams = array(
'q' => '',
'l' => '',
'sort' => 'date',
'radius' => '15',
'st' => '',
'jt' => '',
'start' => '0',
'limit' => '20',
'highlight' => '0',
'filter' => '1',
'fromage' => '1',
'latlong' => '1',
'co' => 'us',
'chnl' => '',
'format' => 'json'
);


/**
* Default constructor; sets the publisher ID and the format
*
* @param integer $pubID Publisher ID from Indeed
* @param string $format Format of data
*/
public function __construct( $pubID, $format = '' ) {

public function __construct( $pubID, $format = '' )
{
// Pass in pubisher ID as integer
$this->pubID = (int)$pubID;

// Check that argument is either `json` or `xml`
if( in_array( strtolower( $format ), array('json', 'xml') ) )
$this->format = strtolower( $format );

if( in_array(strtolower($format), array('json', 'xml')) ) {
$this->setParams( array('format' => strtolower($format)) );
}
}


/**
* Set the default parameters
*
* @param array $params Parameters you want to use
*/
public function setDefaultParams( $params = array() ) {

$this->defaultParams = $params;

public function setParams($params = array())
{
$this->defaultParams = array_merge($this->defaultParams, $params);
}


/**
* Get the default parameters
*
* @return array
*/
public function getDefaultParams() {

public function getParams()
{
return $this->defaultParams;

}


/**
* Query Indeed for jobs
*
* @param mixed $params Job search query or a number of different
* parameters
* @param mixed $params Job search query or a number of different parameters
* @param boolean $raw Return the raw query (document)
*
* @return mixed
*/
public function query( $params, $raw = false ) {

$url = $this->rootURL .
'?publisher=' . $this->pubID .
'&v=' . $this->version .
'&format=' . $this->format;

if( is_array( $params) ){
$url .= $this->makeURI( $params );
} elseif ( is_string( $params ) ) {
$url .= $this->makeURI( array('q' => $params) );
public function query($params, $raw=false)
{
$url = $this->API_URL . '?publisher=' . $this->pubID . '&v=' . $this->version;

if (is_array($params)) {
$url .= $this->makeURI($params);
} elseif (is_string($params)) {
$url .= $this->makeURI(array('q' => $params));
}

$this->url = $url;

if( $raw === false && $this->format === 'json') {
$file = file_get_contents( $url );
$output = json_decode( $file );
} elseif( $raw === false && $this->format === 'xml' ) {
$output = simplexml_load_file( $url );
} else {
$output = file_get_contents( $url );
$this->lastQueryURL = $url;

try {
if ($raw === false && $this->defaultParams['format'] === 'json') {
$results = file_get_contents($url);
$results = json_decode($results, true); // as array
} elseif ($raw === false && $this->defaultParams['format'] === 'xml') {
$results = simplexml_load_file($url);
} else {
$results = file_get_contents($url);
}
} catch(ErrorException $error) {
return $error;
}

return $output;

return $results;
}


/**
* Returns the URL used to request the API
*
* Returns the URL used in last request to API
* @return string
*/
public function getURL() {

return $this->URL;

public function getLastUrl() {
return $this->lastQueryURL;
}


/**
* Builds the URI based on the passed parameters and default
*
* Builds the URI based on the passed and default parameters
* @param array $params Parameters
*
* @return string
*/
private function makeURI( $params = array() ) {

$params = array_merge( $this->defaultParams, $params );
$uri = '';

foreach( $params as $key => $value ) {
if( in_array( $key, $this->acceptedParams ) )
$uri .= '&' . $key . '=' . urlencode( $value );
private function makeURI($params = array())
{
$params = array_merge($this->defaultParams, $params);
$uri = '';

foreach ($params as $key => $value) {
if (isset($this->defaultParams[$key])) {
$uri .= '&' . $key . '=' . urlencode($value);
}
}

if( isset( $_SERVER['REMOTE_ADDR'] ) )
'&userip=' . urlencode( $_SERVER['REMOTE_ADDR'] );

if( isset( $_SERVER['HTTP_USER_AGENT'] ) )
'&useragent=' . urlencode( $_SERVER['HTTP_USER_AGENT'] );
$uri .= '&userip=';
if (isset($_SERVER['REMOTE_ADDR'])) {
$uri .= urlencode($_SERVER['REMOTE_ADDR']);
} elseif (isset($_SERVER['SERVER_ADDR'])) {
$uri .= urlencode($_SERVER['SERVER_ADDR']);
} else {
$uri .= urlencode('1.2.3.4');
}
$uri .= '&useragent=' . (isset($_SERVER['HTTP_USER_AGENT']) ?
urlencode($_SERVER['HTTP_USER_AGENT']) : urlencode('Mozilla/5.0(Firefox)'));

return $uri;

}

}