Passed
Push — master ( b67974...1b1f16 )
by Daniel
03:18
created

Careerjet_API   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 279
rs 10
1
<?php
2
3
/**
4
 * Access Careerjet's job search from PHP
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * LICENSE: This source file is subject to version 3.0 of the PHP license
9
 * that is available through the world-wide-web at the following URI:
10
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
11
 * the PHP License and are unable to obtain it through the web, please
12
 * send a note to [email protected] so we can mail you a copy immediately.
13
 *
14
 *
15
 * @package     Careerjet_API
16
 * @author      Thomas Busch <[email protected]>
17
 * @copyright   2007-2015 Careerjet Limited
18
 * @licence     PHP http://www.php.net/license/3_01.txt
19
 * @version     3.6
20
 * @link        http://www.careerjet.com/partners/api/php/
21
 */
22
23
/**
24
 * Class to access Careerjet's job search API
25
 *
26
 * Code example:
27
 *
28
 * <code>
29
 *
30
 *  require_once "Careerjet_API.php";
31
 *
32
 *  // Create a new instance of the interface for UK job offers
33
 *  $cjapi = new Careerjet_API('en_GB');
34
 *
35
 *
36
 *  // Then call the search methods (see below for parameters)
37
 *  $result = $cjapi->search( array(
38
 *                                   'keywords' => 'java manager',
39
 *                                   'location' => 'London',
40
 *                                   'affid'    => '0afaf0173305e4b9',
41
 *                                 )
42
 *                           );
43
 *
44
 *  if ($result->type == 'JOBS') {
45
 *      echo "Got ".$result->hits." jobs: \n\n";
46
 *      $jobs = $result->jobs;
47
 *
48
 *      foreach ($jobs as &$job) {
49
 *          echo " URL: ".$job->url."\n";
50
 *          echo " TITLE: ".$job->title."\n";
51
 *          echo " LOC:   ".$job->locations."\n";
52
 *          echo " COMPANY: ".$job->company."\n";
53
 *          echo " SALARY: ".$job->salary."\n";
54
 *          echo " DATE:   ".$job->date."\n";
55
 *          echo " DESC:   ".$job->description."\n";
56
 *          echo "\n";
57
 *       }
58
 *   }
59
 *
60
 *  </code>
61
 *
62
 *
63
 * @package    Careerjet_API
64
 * @author     Thomas Busch <[email protected]>
65
 * @copyright  2007-2015 Careerjet Limited
66
 * @link       http://www.careerjet.com/partners/api/php/
67
 */
68
class Careerjet_API {
69
    var $locale = '' ;
70
    var $version = '3.6';
71
    var $careerjet_api_content = '';
72
73
    /**
74
     * Creates client to Careerjet's API
75
     *
76
     * <code>
77
     *  $cjapi = new Careerjet_API($locale);
78
     * </code>
79
     *
80
     * Available locales:
81
     *
82
     * <pre>
83
     *   LOCALE     LANGUAGE         DEFAULT LOCATION     CAREERJET SITE
84
     *   cs_CZ      Czech            Czech Republic       http://www.careerjet.cz
85
     *   da_DK      Danish           Denmark              http://www.careerjet.dk
86
     *   de_AT      German           Austria              http://www.careerjet.at
87
     *   de_CH      German           Switzerland          http://www.careerjet.ch
88
     *   de_DE      German           Germany              http://www.careerjet.de
89
     *   en_AE      English          United Arab Emirates http://www.careerjet.ae
90
     *   en_AU      English          Australia            http://www.careerjet.com.au
91
     *   en_CA      English          Canada               http://www.careerjet.ca
92
     *   en_CN      English          China                http://www.career-jet.cn
93
     *   en_HK      English          Hong Kong            http://www.careerjet.hk
94
     *   en_IE      English          Ireland              http://www.careerjet.ie
95
     *   en_IN      English          India                http://www.careerjet.co.in
96
     *   en_MY      English          Malaysia             http://www.careerjet.com.my
97
     *   en_NZ      English          New Zealand          http://www.careerjet.co.nz
98
     *   en_OM      English          Oman                 http://www.careerjet.com.om
99
     *   en_PH      English          Philippines          http://www.careerjet.ph
100
     *   en_PK      English          Pakistan             http://www.careerjet.com.pk
101
     *   en_QA      English          Qatar                http://www.careerjet.com.qa
102
     *   en_SG      English          Singapore            http://www.careerjet.sg
103
     *   en_GB      English          United Kingdom       http://www.careerjet.com
104
     *   en_US      English          United States        http://www.careerjet.com
105
     *   en_ZA      English          South Africa         http://www.careerjet.co.za
106
     *   en_TW      English          Taiwan               http://www.careerjet.com.tw
107
     *   en_VN      English          Vietnam              http://www.careerjet.vn
108
     *   es_AR      Spanish          Argentina            http://www.opcionempleo.com.ar
109
     *   es_BO      Spanish          Bolivia              http://www.opcionempleo.com.bo
110
     *   es_CL      Spanish          Chile                http://www.opcionempleo.cl
111
     *   es_CR      Spanish          Costa Rica           http://www.opcionempleo.co.cr
112
     *   es_DO      Spanish          Dominican Republic   http://www.opcionempleo.com.do
113
     *   es_EC      Spanish          Ecuador              http://www.opcionempleo.ec
114
     *   es_ES      Spanish          Spain                http://www.opcionempleo.com
115
     *   es_GT      Spanish          Guatemala            http://www.opcionempleo.com.gt
116
     *   es_MX      Spanish          Mexico               http://www.opcionempleo.com.mx
117
     *   es_PA      Spanish          Panama               http://www.opcionempleo.com.pa
118
     *   es_PE      Spanish          Peru                 http://www.opcionempleo.com.pe
119
     *   es_PR      Spanish          Puerto Rico          http://www.opcionempleo.com.pr
120
     *   es_PY      Spanish          Paraguay             http://www.opcionempleo.com.py
121
     *   es_UY      Spanish          Uruguay              http://www.opcionempleo.com.uy
122
     *   es_VE      Spanish          Venezuela            http://www.opcionempleo.com.ve
123
     *   fi_FI      Finnish          Finland              http://www.careerjet.fi
124
     *   fr_CA      French           Canada               http://www.option-carriere.ca
125
     *   fr_BE      French           Belgium              http://www.optioncarriere.be
126
     *   fr_CH      French           Switzerland          http://www.optioncarriere.ch
127
     *   fr_FR      French           France               http://www.optioncarriere.com
128
     *   fr_LU      French           Luxembourg           http://www.optioncarriere.lu
129
     *   fr_MA      French           Morocco              http://www.optioncarriere.ma
130
     *   hu_HU      Hungarian        Hungary              http://www.careerjet.hu
131
     *   it_IT      Italian          Italy                http://www.careerjet.it
132
     *   ja_JP      Japanese         Japan                http://www.careerjet.jp
133
     *   ko_KR      Korean           Korea                http://www.careerjet.co.kr
134
     *   nl_BE      Dutch            Belgium              http://www.careerjet.be
135
     *   nl_NL      Dutch            Netherlands          http://www.careerjet.nl
136
     *   no_NO      Norwegian        Norway               http://www.careerjet.no
137
     *   pl_PL      Polish           Poland               http://www.careerjet.pl
138
     *   pt_PT      Portuguese       Portugal             http://www.careerjet.pt
139
     *   pt_BR      Portuguese       Brazil               http://www.careerjet.com.br
140
     *   ru_RU      Russian          Russia               http://www.careerjet.ru
141
     *   ru_UA      Russian          Ukraine              http://www.careerjet.com.ua
142
     *   sv_SE      Swedish          Sweden               http://www.careerjet.se
143
     *   sk_SK      Slovak           Slovakia             http://www.careerjet.sk
144
     *   tr_TR      Turkish          Turkey               http://www.careerjet.com.tr
145
     *   uk_UA      Ukrainian        Ukraine              http://www.careerjet.ua
146
     *   vi_VN      Vietnamese       Vietnam              http://www.careerjet.com.vn
147
     *   zh_CN      Chinese          China                http://www.careerjet.cn
148
     * </pre>
149
     *
150
     * @param string $locale
151
     */
152
    function __construct($locale = 'en_GB')
153
    {
154
        $this->locale = $locale;
155
    }
156
157
    /**
158
     * @ignore
159
     **/
160
    function call($fname , $args)
161
    {
162
        $url = 'http://public.api.careerjet.net/'.$fname.'?locale_code='.$this->locale;
163
164
        if (empty($args['affid'])) {
165
            return (object) array(
166
                'type' => 'ERROR',
167
                'error' => "Your Careerjet affiliate ID needs to be supplied. If you don't " .
168
                    "have one, open a free Careerjet partner account."
169
            );
170
        }
171
172
        foreach ($args as $key => $value) {
173
            $url .= '&'. $key . '='. urlencode($value);
174
        }
175
176
        if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
177
            $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
178
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
179
            $ip = $_SERVER['HTTP_CLIENT_IP'];
180
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
181
            // For more info: http://en.wikipedia.org/wiki/X-Forwarded-For
182
            $ip = trim(array_shift(array_values(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))));
183
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
184
            $ip = $_SERVER['REMOTE_ADDR'];
185
        } else {
186
            $ip = '::1';
187
        }
188
189
        $url .= '&user_ip=' . $ip;
190
191
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
192
            $userAgent = urlencode($_SERVER['HTTP_USER_AGENT']);
193
        } else {
194
            $userAgent = 'Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_10_5%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F48.0.2564.103+Safari%2F537.36';
195
        }
196
        $url .= '&user_agent=' . $userAgent;
197
198
        // determine current page
199
        $current_page_url = '';
200
        if (!empty ($_SERVER["SERVER_NAME"]) && !empty ($_SERVER["REQUEST_URI"])) {
201
            $current_page_url = 'http';
202
            if (!empty ($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
203
                $current_page_url .= "s";
204
            }
205
            $current_page_url .= "://";
206
207
            if (!empty ($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] != "80") {
208
                $current_page_url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
209
            } else {
210
                $current_page_url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
211
            }
212
        }
213
214
        $header = "User-Agent: careerjet-api-client-v" . $this->version . "-php-v" . phpversion();
215
        if ($current_page_url) {
216
            $header .= "\nReferer: " . $current_page_url;
217
        }
218
219
        $careerjet_api_context = stream_context_create(array(
220
            'http' => array('header' => $header)
221
        ));
222
223
        $response = file_get_contents($url, false, $careerjet_api_context);
224
        return json_decode($response);
225
    }
226
227
    /**
228
     * Performs a search using Careerjet's public search API
229
     *
230
     * Code example:
231
     *
232
     * <code>
233
     *   $result = $cjapi->search(array(
234
     *                                   'keywords'   => 'java',
235
     *                                   'location'   => 'London',
236
     *                                   'pagesize'   => 10,
237
     *                                   'affid'      => '0afaf0173305e4b9',
238
     *                                 )
239
     *                          );
240
     * </code>
241
     *
242
     * If the given location is not ambiguous, you can use this object like that:
243
     *
244
     * <code>
245
     *   if ($result->type == 'JOBS') {
246
     *      echo "Got ".$result->hits." jobs: \n";
247
     *      echo " On ".$result->pages." pages \n";
248
     *      $jobs = $result->jobs;
249
     *
250
     *      foreach ($jobs as &$job) {
251
     *          echo " URL: ".$job->url."\n";
252
     *          echo " TITLE: ".$job->title."\n";
253
     *          echo " LOC:   ".$job->locations."\n";
254
     *          echo " COMPANY: ".$job->company."\n";
255
     *          echo " SALARY: ".$job->salary."\n";
256
     *          echo " DATE:   ".$job->date."\n";
257
     *          echo " DESC:   ".$job->description."\n";
258
     *          echo " SITE:   ".$job->site."\n";
259
     *          echo "\n" ;
260
     *       }
261
     *   }
262
     * </code>
263
     *
264
     * If the given location is ambiguous, result contains a list of suggested locations:
265
     *
266
     * <code>
267
     *   if ($result->type == 'LOCATIONS') {
268
     *       echo "Suggested locations:\n";
269
     *       $locations = $result->locations;
270
     *
271
     *       foreach ($locations as &$loc) {
272
     *           echo $loc."\n" ;
273
     *       }
274
     *   }
275
     * </code>
276
     *
277
     * @param   array $args
278
     *
279
     * map of search parameters
280
     *
281
     * Example: array( 'keywords' => 'java manager',
282
     *                 'location' => 'london', ... );
283
     *
284
     * All values of keys MUST be encoded either in ASCII or UTF8.
285
     * If you use this API within a webpage, make sure:
286
     *   - That your pages are served in utf-8 encoding OR
287
     *   - Your job search form begins like that :
288
     *        <form accept-charset="UTF-8"
289
     *
290
     *
291
     * MANDATORY PARAMETERS
292
     *
293
     * The following parameters is mandatory:
294
     *  - <b>affid:</b><br>
295
     *    Affiliate ID provided by Careerjet<br>
296
     *    Requires to open a Careerjet partner account<br>
297
     *    http://www.careerjet.com/partners/
298
     *
299
     * FILTERS
300
     *
301
     * All filters have default values and are not mandatory:
302
     *  - <b>keywords:</b><br>
303
     *    Keywords to search in job offers. Example: 'java manager'<br>
304
     *    Default: none (Returns all offers from default country)
305
     *  - <b>location:</b><br>
306
     *    Location to search job offers in. Examples: 'London', 'Paris'<br>
307
     *    Default: none (Returns all offers from default country)
308
     *  - <b>sort:</b><br>
309
     *    Type of sort.<br>
310
     *    Available values are 'relevance' (default), 'date', and 'salary'.
311
     *  - <b>start_num:</b><br>
312
     *    Num of first offer returned in entire result space should be >= 1 and <= Number of hits<br>
313
     *    Default: 1
314
     *  - <b>pagesize:</b><br>
315
     *    Number of offers returned in one call<br>
316
     *    Default: 20
317
     *  - <b>page:</b><br>
318
     *    Current page number (should be >=1)<br>
319
     *    If set, will override start_num<br>
320
     *    The maxumum number of page is given by $result->pages
321
     *  - <b>contracttype:</b><br>
322
     *    Character code for contract types:<br>
323
     *    'p'    - permanent job<br>
324
     *    'c'    - contract<br>
325
     *    't'    - temporary<br>
326
     *    'i'    - training<br>
327
     *    'v'    - voluntary<br>
328
     *    Default: none (all contract types)
329
     *  - <b>contractperiod:</b><br>
330
     *    Character code for contract contract periods:<br>
331
     *    'f'     - Full time<br>
332
     *    'p'     - Part time<br>
333
     *    Default: none (all contract periods)
334
     * @return object An object containing results
335
     *
336
     * @throws \Jobles\Careerjet\Exception\CareerjetException
337
     */
338
    function search($args)
339
    {
340
        $result =  $this->call('search' , $args);
341
        if ($result->type == 'ERROR') {
342
            throw new \Jobles\Careerjet\Exception\CareerjetException($result->error);
343
        }
344
        return $result;
345
    }
346
}
347