From 931f96d57efe797829ac83f91d827558c4e3adad Mon Sep 17 00:00:00 2001 From: adamasantares Date: Sat, 7 Feb 2015 12:48:46 +0600 Subject: [PATCH 1/7] Full refactoring and fix the error with user-agent and userip. --- README.md | 10 ++- indeed-api.php | 179 ++++++++++++++++++++----------------------------- 2 files changed, 81 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index 8129768..524e748 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Example of Usage require 'indeed-api.php'; $indeedAPI = new IndeedAPI( 123455667 ); - $indeedAPI->setDefaultParams( array( + $indeedAPI->setParams( array( 'co' => 'gb' ) ); @@ -38,11 +38,16 @@ Author(s) - Neil Sweeney +Contributor(s) +------------------------------------------------------------------------------- + +- AdamasAntares https://github.com/adamasantares + Releases ------------------------------------------------------------------------------- -### 1.0.0 (2013-11-21) +### 1.0.8 (2015-02-07) Initial release @@ -50,3 +55,4 @@ Initial release * Pass in default values * Choose between JSON or XML feed * Choose to return object or raw document +* Fix useragent and userip for CLI diff --git a/indeed-api.php b/indeed-api.php index 2806297..548957b 100644 --- a/indeed-api.php +++ b/indeed-api.php @@ -1,199 +1,166 @@ '', + 'l' => '', + 'sort' => 'date', + 'radius' => '15', + 'st' => '', + 'jt' => '', + 'start' => '0', + 'limit' => '20', + 'highlight' => '0', + 'filter' => '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; + $this->lastQueryURL = $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 ); + 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 { - $output = file_get_contents( $url ); + $results = file_get_contents($url); } - 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; - } } From 92b76c9c60a7589561ac6d3a402895ba9f2fb391 Mon Sep 17 00:00:00 2001 From: adamasantares Date: Sun, 8 Feb 2015 12:14:30 +0600 Subject: [PATCH 2/7] add "fromage" parameter --- indeed-api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/indeed-api.php b/indeed-api.php index 548957b..4ec576d 100644 --- a/indeed-api.php +++ b/indeed-api.php @@ -51,6 +51,7 @@ class IndeedAPI { 'limit' => '20', 'highlight' => '0', 'filter' => '1', + 'fromage' => '1', 'latlong' => '1', 'co' => 'us', 'chnl' => '', From 42f77e4adbc606ec08aab756619a02779f33cde1 Mon Sep 17 00:00:00 2001 From: adamasantares Date: Sun, 8 Feb 2015 19:15:47 +0600 Subject: [PATCH 3/7] add try{} block for catch loading errors --- indeed-api.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/indeed-api.php b/indeed-api.php index 4ec576d..0cffed7 100644 --- a/indeed-api.php +++ b/indeed-api.php @@ -113,13 +113,17 @@ public function query($params, $raw=false) $this->lastQueryURL = $url; - 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); + 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 $results; From 2655a5608ae078f05ba8238b306fc88d2ee37a8d Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 17 Feb 2015 23:02:41 +0500 Subject: [PATCH 4/7] Update README.md --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 524e748..35d634a 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,13 @@ 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``` +- 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``` Example of Usage From 429be105c9dc4ebcb8a7542f85a7d69267ba30ba Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 17 Feb 2015 23:03:00 +0500 Subject: [PATCH 5/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35d634a..34f8d01 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ PHP interface to Indeed job search API. Description about queries format you can see there https://ads.indeed.com/jobroll/xmlfeed -But In original documentation you will not find this +But in original documentation you will not find this - Query for search specific job by him jobkey: ```q=jobkey:c798c4fed37edf59``` - Query by title: ```q=title:Title+of+job``` will returns all jobs matched with this title From ea264f2011dc390dc85927c391637070f20a126b Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 17 Feb 2015 23:05:49 +0500 Subject: [PATCH 6/7] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 34f8d01..6ae2f60 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ But in original documentation you will not find this - 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 ------------------------------------------------------------------------------- From 5d54ae26716a04fc8111235ccb0546f26a18d360 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 18 Feb 2015 01:40:44 +0500 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ae2f60..50c2ca1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Description about queries format you can see there https://ads.indeed.com/jobrol But in original documentation you will not find this -- Query for search specific job by him jobkey: ```q=jobkey:c798c4fed37edf59``` +- 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```