Subversion Repositories Web Services

Rev

Rev 119243 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82190 hhl 1
<?php
87714 hhl 2
 
82190 hhl 3
/**
4
 *
5
 * This file is part of Open Library System.
6
 * Copyright ¬© 2009, Dansk Bibliotekscenter a/s,
7
 * Tempovej 7-11, DK-2750 Ballerup, Denmark. CVR: 15149043
8
 *
9
 * Open Library System is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Open Library System is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with Open Library System.  If not, see <http://www.gnu.org/licenses/>.
87714 hhl 21
 */
82190 hhl 22
/**
23
 * Default global options
24
 * Should be defined in some config-file
25
 * @var    mixed
26
 */
27
$curl_default_options = array(
118290 fvs 28
  CURLOPT_TIMEOUT => 30,
29
  CURLOPT_HEADER => FALSE,
30
  CURLOPT_RETURNTRANSFER => TRUE
87714 hhl 31
);
82190 hhl 32
 
33
/**
87714 hhl 34
 * \brief Class for handling cURL
35
 *
36
 * Example:
37
 *
38
 * $curl = new curl(); \n
39
 * print_r($curl->get(array("http://checkip.dbc.dk/","http://no.such_domain.net"))); // returns array \n
40
 * $curl->close(); \n
41
 *
42
 * Example:
43
 *
44
 * $curl = new curl(); \n
45
 * $curl->set_url("http://checkip.dbc.dk/",0);     // returns TRUE | FALSE \n
46
 * $handle_id = $curl->get_next_handle(); \n
47
 * $curl->set_url("http://no.such_domain.net",$handle_id);  // returns TRUE | FALSE \n
48
 * print_r($curl->get());                          // returns array \n
49
 * $curl->close(); \n
50
 *
51
 * Example:
52
 *
53
 * $curl = new curl(); \n
54
 * print_r($curl->get("http://checkip.dbc.dk/"));     // returns string \n
55
 * print_r($curl->get("http://kundeservice.dbc.dk")); // returns string \n
56
 * print_r($curl->get("http://no.such_domain.net"));  // returns string \n
57
 * $curl->close(); \n
58
 *
59
 * Example:
60
 *
61
 * $curl = new curl(); \n
62
 * set_url("http://lxr.php.net/");                 // returns TRUE | FALSE \n
63
 * set_option(CURLOPT_PROXY,"phobos.dbc.dk:3128"); // returns TRUE | FALSE \n
64
 * echo $res = $curl->get();                       // returns string \n
65
 * $curl->get_option();                            // returns array \n
66
 * $curl->get_status();                            // returns array \n
67
 * $curl->get_status('http_code');                 // returns string \n
68
 * $curl->has_error();                             // returns string | FALSE \n
69
 * $curl->close(); \n
70
 *
71
 * Example:
72
 *
73
 * $curl = new curl(); \n
74
 * $curl->set_multiple_options($options_array)  // returns TRUE | FALSE \n
75
 * $curl->set_option($option,$value,$n)         // returns TRUE | FALSE \n
76
 * $curl->set_proxy("phobos.dbc.dk:3128", $n)   // returns TRUE | FALSE \n
77
 * $curl->set_url("http://lxr.php.net/");       // returns TRUE | FALSE \n
78
 * $res = $curl->get();                         // returns array \n
79
 * $curl->get_option();                         // returns array \n
80
 * $curl->get_option(CURLOPT_URL);              // returns array \n
81
 * $curl->get_option(CURLOPT_PROXY,$n);         // returns string \n
82
 * $curl->get_status();                         // returns array \n
83
 * $curl->get_status('http_code');              // returns array \n
84
 * $curl->get_status('http_code',$n);           // returns string \n
85
 * $curl->has_error();                          // returns array \n
86
 * $curl->has_error($n);                        // returns string | FALSE \n
87
 * $curl->close(); \n
88
 *
89
 *
90
 * Example:
91
 * $curl = new curl(); \n
92
 * $curl->set_timeout(10);                    // returns TRUE | FALSE \n
93
 * $curl->set_proxy("someproxy.dk:1020", $n); // returns TRUE | FALSE \n
94
 * $curl->set_post_xml("<xml>foobar</xml>");  // returns TRUE | FALSE \n
95
 * $res = $curl->get();                       // returns array \n
96
 * $curl->close(); \n
97
 *
98
 *
99
 * Example:
100
 * $curl = new curl(); \n
101
 * $curl->set_post(array("foo" => "bar"); // returns TRUE | FALSE \n
102
 * $res = $curl->get();                   // returns array \n
103
 * $curl->close(); \n
104
 *
105
 */
82190 hhl 106
class Curl {
118290 fvs 107
  ///////////////////////////////////////
108
  // PRIVATE VARIABLES DO NOT CHANGE!!!//
109
  ///////////////////////////////////////
82190 hhl 110
 
118290 fvs 111
  /**
112
   * The handle(s) for the current curl session.
113
   * @access private
114
   * @var    mixed
115
   */
116
  private $curl_multi_handle;
82190 hhl 117
 
118290 fvs 118
  /**
119
   * Status information for the last executed http request.  Includes the errno and error
120
   * in addition to the information returned by curl_getinfo.
121
   *
122
   * The keys defined are those returned by curl_getinfo with two additional
123
   * ones specified, 'error' which is the value of curl_error and 'errno' which
124
   * is the value of curl_errno.
125
   *
126
   * @link http://www.php.net/curl_getinfo @endlink
127
   * @link http://www.php.net/curl_errno @endlink
128
   * @link http://www.php.net/curl_error @endlink
129
   * @access private
130
   * @var mixed
131
   */
132
  private $curl_status;
82190 hhl 133
 
118290 fvs 134
  /**
135
   * Current setting of the curl options.
136
   *
137
   * @access private
138
   * @var mixed
139
   */
140
  private $curl_options;
141
  private $cookies = array();
82190 hhl 142
 
118290 fvs 143
  ////////////////////
144
  // PUBLIC METHODS //
145
  ////////////////////
82190 hhl 146
 
118290 fvs 147
  /**
148
   * curl class constructor
149
   *
150
   * Initializes the curl class
151
   * @link http://www.php.net/curl_init @endlink
152
   * @param $url [optional] the URL to be accessed by this instance of the class. (string)
153
   * @return mixed
154
   */
155
  public function __construct($url = NULL) {
156
    global $curl_default_options;
82190 hhl 157
 
118290 fvs 158
    $this->curl_options = null;
159
    $this->curl_status = null;
160
    $this->wait_for_connections = PHP_INT_MAX;
82190 hhl 161
 
118290 fvs 162
    if (!function_exists('curl_init')) {
120114 fvs 163
      self::local_verbose(FATAL, "PHP was not built with curl, rebuild PHP to use the curl class.");
118290 fvs 164
      return FALSE;
165
    }
82190 hhl 166
 
118290 fvs 167
    if (!isset($curl_default_options)) {
120114 fvs 168
      self::local_verbose(ERROR, '$curl_default_options is not defined. See the class description for usage');
118290 fvs 169
      return FALSE;
170
    }
171
    else
172
      $this->curl_default_options = $curl_default_options;
82190 hhl 173
 
118290 fvs 174
    $this->curl_handle[] = curl_init();
82190 hhl 175
 
118290 fvs 176
    $this->set_multiple_options($this->curl_default_options);
177
    return TRUE;
178
  }
179
 
180
  /**
181
   * Set multiple options for a cURL transfer
182
   *
183
   * @link http://dk2.php.net/curl_setopt_array @endlink
184
   * @param $options - The array of curl options. See $curl_default_options (array)
185
   * @return bool  Returns TRUE if all options were successfully set (on all handles).
186
   *               If an option could not be successfully set, FALSE is immediately returned,
187
   *               ignoring any future options in the options array.
188
   */
189
  public function set_multiple_options($options = NULL) {
190
 
191
    if (!$options)
192
      return FALSE;
193
 
194
    foreach ($this->curl_handle as $key => $handle) {
195
      $res = curl_setopt_array($this->curl_handle[$key], $options);
196
      if (!$res)
197
        return FALSE;
82190 hhl 198
    }
118290 fvs 199
    reset($this->curl_handle);
200
    foreach ($this->curl_handle as $key => $handle) {
201
      foreach ($options as $option => $value) {
202
        $this->curl_options[$key][$option] = $value;
203
      }
204
    }
205
    return TRUE;
206
  }
82190 hhl 207
 
118290 fvs 208
  /**
209
   * Execute the curl request and return the result.
210
   *
211
   * @link http://www.php.net/curl_multi_close @endlink \n
212
   * @link http://www.php.net/curl_multi_init @endlink \n
213
   * @link http://www.php.net/curl_multi_add_handle @endlink \n
214
   * @link http://www.php.net/curl_multi_exec @endlink \n
215
   * @link http://www.php.net/curl_multi_getcontent @endlink \n
216
   * @link http://www.php.net/curl_getinfo @endlink \n
217
   * @link http://www.php.net/curl_errno @endlink \n
218
   * @link http://www.php.net/curl_error @endlink \n
219
   * @param boolean $urls
220
   * @return string The contents of the page (or other interaction as defined by the
221
   *                settings of the various curl options).
222
   */
223
  public function get($urls = FALSE) {
82190 hhl 224
 
118290 fvs 225
    if ($urls)
226
      self::set_url($urls);
82190 hhl 227
 
118290 fvs 228
    // remove previous curl_multi_handle, if any
229
    if (is_resource($this->curl_multi_handle)) {
230
      if (is_array($this->curl_handle)) {
87714 hhl 231
        foreach ($this->curl_handle as $key => $handle) {
118290 fvs 232
          curl_multi_remove_handle($this->curl_multi_handle, $this->curl_handle[$key]);
87714 hhl 233
        }
118290 fvs 234
      }
235
    }
236
    else {
237
      //create a new multiple cURL handle
238
      $this->curl_multi_handle = curl_multi_init();
239
    }
240
 
241
    // set cookies and add the handles
242
    foreach ($this->curl_handle as $key => $handle) {
243
      if (array_key_exists(intval($handle), $this->cookies)) {
244
        if ($this->cookies[intval($handle)]) {
245
          self::set_option(CURLOPT_COOKIE, implode(';', $this->cookies[$handle]));
87714 hhl 246
        }
118290 fvs 247
      }
248
      curl_multi_add_handle($this->curl_multi_handle, $this->curl_handle[$key]);
87714 hhl 249
    }
82190 hhl 250
 
118290 fvs 251
    // execute the handles
252
    do {
253
      /*
254
        curl_multi_select() should according to the manual:
255
        'Blocks until there is activity on any of the curl_multi connections.'
256
        but including the line below without an timeout, more than doubles the time used in this function???
82190 hhl 257
 
118290 fvs 258
        Has to call it with a timeout less than 1, or it will apparently default (and wait) 1 second for
259
        each connection????????
260
       */
261
      curl_multi_select($this->curl_multi_handle, 0.01);
262
      $status = curl_multi_exec($this->curl_multi_handle, $active);
263
      if ($info = curl_multi_info_read($this->curl_multi_handle)) {
264
        $multi_status[intval($info['handle'])] = $info['result'];
265
        if (curl_getinfo($info['handle'], CURLINFO_HTTP_CODE) == 200)
266
          $this->wait_for_connections--;
267
      }
268
    } while ($this->wait_for_connections && ($status === CURLM_CALL_MULTI_PERFORM || $active));
269
    if ($info = curl_multi_info_read($this->curl_multi_handle)) {
270
      $multi_status[intval($info['handle'])] = $info['result'];
271
    }
272
    foreach ($this->curl_handle as $key => $handle) {
273
      $this->curl_status[$key] = curl_getinfo($this->curl_handle[$key]);
274
      $this->curl_status[$key]['errno'] = curl_errno($this->curl_handle[$key]);
275
      $this->curl_status[$key]['error'] = curl_error($this->curl_handle[$key]);
276
      if (empty($this->curl_status[$key]['errno']) && isset($multi_status[$handle])) {
277
        $this->curl_status[$key]['errno'] = $multi_status[intval($handle)];
278
      }
279
      if ($this->curl_status[$key]['errno'] == 28) { // CURLE_OPERATION_TIMEDOUT
280
        $this->curl_status[$key]['http_code'] = 504;  // Gateway timeout
281
      }
282
    }
283
    foreach ($this->curl_handle as $key => $handle) {
284
      $this->curl_content[$key] = curl_multi_getcontent($handle);
285
      if (method_exists('curl_recorder', 'record')) {
286
        $curl_recorder->record('status', $this->curl_status[$key], $key);
287
        $curl_recorder->record('result', $this->curl_content[$key], $key);
288
      }
289
    }
82190 hhl 290
 
118290 fvs 291
    if (sizeof($this->curl_handle) == 1)
292
      return $this->curl_content[0];
293
    else
294
      return $this->curl_content;
295
  }
82190 hhl 296
 
118290 fvs 297
  /**
298
   * Returns the current setting of the request option.
299
   * If no handle_number has been set, it return the settings of all handles.
300
   *
301
   * @param $option - One of the valid CURLOPT defines. (mixed)
302
   * @param $handle_no - Handle number. (integer)
303
   * @returns mixed
304
   */
305
  public function get_option($option = null, $handle_no = 0) {
82190 hhl 306
 
118290 fvs 307
    foreach ($this->curl_handle as $key => $handle) {
308
      if (!$handle_no || $key == $handle_no) {
309
        if (empty($option)) {
310
          $option_values[] = $this->curl_options[$key];
87714 hhl 311
        }
118290 fvs 312
        else {
313
          if (isset($this->curl_options[$key][$option]))
314
            $option_values[] = $this->curl_options[$key][$option];
315
          else
316
            $option_values[] = null;
87714 hhl 317
        }
118290 fvs 318
      }
82190 hhl 319
    }
320
 
118290 fvs 321
    if ($handle_no || sizeof($this->curl_handle) == 1)
322
      return $option_values[0];
323
    else
324
      return $option_values;
325
  }
82190 hhl 326
 
118290 fvs 327
  /**
328
   * Set a curl option.
329
   *
330
   * @link http://www.php.net/curl_setopt @endlink
331
   * @param $option - One of the valid CURLOPT defines. (mixed)
332
   * @param $value - The value of the curl option. (mixed)
333
   * @param $handle_no - Handle number. (integer)
334
   * @return boolean
335
   */
336
  public function set_option($option, $value, $handle_no = null) {
82190 hhl 337
 
118290 fvs 338
    if ($handle_no === null) {
339
      foreach ($this->curl_handle as $key => $handle) {
340
        $this->curl_options[$key][$option] = $value;
341
        $res = curl_setopt($this->curl_handle[$key], $option, $value);
342
        if (!$res)
343
          return FALSE;
344
      }
82190 hhl 345
    }
118290 fvs 346
    else {
347
      self::handle_check($handle_no);
348
      $this->curl_options[$handle_no][$option] = $value;
349
      $res = curl_setopt($this->curl_handle[$handle_no], $option, $value);
82190 hhl 350
    }
118290 fvs 351
    return $res;
352
  }
82190 hhl 353
 
118290 fvs 354
  /**
355
   * Set CURLOPT_URL value(s).
356
   * @param $value (s)   - The value of the curl option. (mixed)
357
   * @param $handle_no - Handle number. Default 0. (integer)
358
   * @return void
359
   */
360
  public function set_url($value, $handle_no = 0) {
361
    if (is_array($value)) {
362
      foreach ($value as $key => $url) {
363
        self::set_option(CURLOPT_URL, $url, $key);
364
        if (method_exists('curl_recorder', 'record_url')) {
365
          $curl_recorder->record_url($url, $key);
87714 hhl 366
        }
118290 fvs 367
      }
87714 hhl 368
    }
118290 fvs 369
    else {
370
      self::set_option(CURLOPT_URL, $value, $handle_no);
371
      if (method_exists('curl_recorder', 'record_url')) {
372
        $curl_recorder->record_url($value, $handle_no);
373
      }
87714 hhl 374
    }
118290 fvs 375
  }
82190 hhl 376
 
118290 fvs 377
  /**
378
   * Set HTTP proxy value(s).
379
   * @param $value - HTTP proxy
380
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
381
   * @return boolean
382
   */
383
  public function set_proxy($value, $handle_no = null) {
384
    if ($ret = self::set_option(CURLOPT_HTTPPROXYTUNNEL, TRUE, $handle_no))
385
      $ret = self::set_option(CURLOPT_PROXY, $value, $handle_no);
386
    return $ret;
387
  }
82190 hhl 388
 
118290 fvs 389
  /**
390
   * Set using cookies
391
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
392
   * @return boolean
393
   */
394
  public function use_cookies($handle_no = null) {
395
    return self::set_option(CURLOPT_HEADERFUNCTION, array($this, 'callback_save_cookies'), $handle_no);
396
  }
82190 hhl 397
 
118290 fvs 398
  /**
399
   * Set HTTP authentication value(s).
400
   * @param $user - HTTP user
401
   * @param $passwd - HTTP password
402
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
403
   * @return boolean
404
   */
405
  public function set_authentication($user, $passwd, $handle_no = null) {
406
    return self::set_option(CURLOPT_USERPWD, $user . ':' . $passwd, $handle_no);
407
  }
82190 hhl 408
 
118290 fvs 409
  /**
410
   * Set HTTP proxy authentication value(s).
411
   * @param $user - HTTP proxy user
412
   * @param $passwd - HTTP proxy password
413
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
414
   * @return boolean
415
   */
416
  public function set_proxy_authentication($user, $passwd, $handle_no = null) {
417
    return self::set_option(CURLOPT_PROXYUSERPWD, '[' . $user . ']:[' . $passwd . ']', $handle_no);
418
  }
82190 hhl 419
 
118290 fvs 420
  /**
421
   * Set timeout
422
   * @param $seconds - timeout ind seconds
423
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
424
   * @return boolean
425
   */
426
  public function set_timeout($seconds, $handle_no = null) {
427
    return self::set_option(CURLOPT_TIMEOUT, $seconds, $handle_no);
428
  }
82190 hhl 429
 
118290 fvs 430
  /**
431
   * Set number of connections to wait for
432
   * @param $wait_for_connections - max connections to wait for
433
   * @return boolean
434
   */
435
  public function set_wait_for_connections($wait_for_connections) {
436
    $this->wait_for_connections = $wait_for_connections;
437
    return TRUE;
438
  }
87714 hhl 439
 
118290 fvs 440
  /**
441
   * Set POST value(s).
442
   * @param $value - The value(s) to post
443
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
444
   * @return boolean
445
   */
446
  public function set_post($value, $handle_no = null) {
447
    if ($ret = self::set_option(CURLOPT_POST, 1, $handle_no))
448
      $ret = self::set_option(CURLOPT_POSTFIELDS, $value, $handle_no);
449
    if (method_exists('curl_recorder', 'record_parameters')) {
450
      $curl_recorder->record_parameters($value, $handle_no);
82190 hhl 451
    }
118290 fvs 452
    return $ret;
453
  }
82190 hhl 454
 
118290 fvs 455
  /**
456
   * Set POST value(s) with Content-Type: text/xml.
457
   * @param $value - The value(s) to post
458
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
459
   * @return boolean
460
   */
461
  public function set_post_xml($value, $handle_no = null) {
462
    return self::set_post_with_header($value, 'Content-Type: text/xml', $handle_no);
463
  }
82190 hhl 464
 
118290 fvs 465
  /**
466
   * Set POST value(s) with Content-Type: application/xml.
467
   * @param $value - The value(s) to post
468
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
469
   * @return boolean
470
   */
471
  public function set_post_application_xml($value, $handle_no = null) {
472
    return self::set_post_with_header($value, 'Content-Type: application/xml', $handle_no);
473
  }
82190 hhl 474
 
118290 fvs 475
  /**
476
   * Set POST value(s).
477
   * @param $header_line - Like 'Content-Type: ...'
478
   * @param string $value - The value(s) to post
479
   * @param string $header_line - Like 'Content-Type: ...'
480
   * @param integer $handle_no - Handle number. Default all handle numbers. (integer)
481
   * @return boolean
482
   */
483
  public function set_post_with_header($value, $header_line, $handle_no = null) {
484
    $headers = $this->get_option(CURLOPT_HTTPHEADER, $handle_no);
485
    $headers[] = $header_line;
486
    if ($ret = self::set_option(CURLOPT_HTTPHEADER, $headers, $handle_no))
487
      $ret = self::set_post($value, $handle_no);
488
    return $ret;
489
  }
490
 
491
  /**
492
   * Set SOAP Action
493
   * @param string $action - The soap-action
494
   * @param integer $handle_no - Handle number. Default all handle numbers. (integer)
495
   * @return boolean
496
   */
497
  public function set_soap_action($action, $handle_no = null) {
498
    $headers = $this->get_option(CURLOPT_HTTPHEADER, $handle_no);
499
    $headers[] = "SOAPAction: " . $action;
500
    return self::set_option(CURLOPT_HTTPHEADER, $headers, $handle_no);
501
  }
502
 
503
  /**
504
   * Get next available handle ID.
505
   * @return integer
506
   */
507
  public function get_next_handle() {
508
    $next_handle_no = 0;
509
    foreach ($this->curl_handle as $key => $handle) {
510
      if ($key > $next_handle_no)
511
        $next_handle_no = $key;
82190 hhl 512
    }
118290 fvs 513
    return $next_handle_no + 1;
514
  }
82190 hhl 515
 
118290 fvs 516
  /**
517
   * Return the status information of the last curl request.
518
   *
519
   * @param string $field [optional] the particular portion (string)
520
   *                     of the status information desired.
521
   *                     If omitted the array of status
522
   *                     information is returned.  If a non-existent
523
   *                     status field is requested, FALSE is returned.
524
   * @param integer $handle_no  Handle number. (integer)
525
   * @return mixed
526
   */
527
  public function get_status($field = null, $handle_no = 0) {
528
 
529
    foreach ($this->curl_handle as $key => $handle) {
530
      if (!$handle_no || $key == $handle_no) {
531
        if (empty($field)) {
532
          $status[] = $this->curl_status[$key];
82190 hhl 533
        }
118290 fvs 534
        else {
535
          if (isset($this->curl_status[$key][$field])) {
536
            $status[] = $this->curl_status[$key][$field];
537
          }
538
          else
539
            return FALSE;
540
        }
541
      }
82190 hhl 542
    }
543
 
118290 fvs 544
    if ($handle_no || sizeof($this->curl_handle) == 1)
545
      return $status[0];
546
    else
547
      return $status;
548
  }
82190 hhl 549
 
118290 fvs 550
  /**
551
   * Did the last curl exec operation have an error?
552
   *
553
   * @param $handle_no - Handle number. (integer)
554
   * @return mixed  The error message associated with the error if an error
555
   *                occurred, FALSE otherwise.
556
   */
557
  public function has_error($handle_no = 0) {
558
 
559
    foreach ($this->curl_handle as $key => $handle) {
560
      if (!$handle_no || $key == $handle_no) {
561
        if (isset($this->curl_status[$key]['error'])) {
562
          $has_error[] = (empty($this->curl_status[$key]['error']) ? FALSE : $this->curl_status[$key]['error']);
87714 hhl 563
        }
564
        else
118290 fvs 565
          $has_error[] = FALSE;
566
      }
87714 hhl 567
    }
82190 hhl 568
 
118290 fvs 569
    if ($handle_no || sizeof($this->curl_handle) == 1)
570
      return $has_error[0];
571
    else
572
      return $has_error;
573
  }
82190 hhl 574
 
118290 fvs 575
  /**
576
   * Free the resources associated with the curl session.
577
   *
578
   * @link http://www.php.net/curl_close @endlink
579
   * $return void
580
   */
581
  public function close() {
582
    foreach ($this->curl_handle as $key => $handle) {
583
      curl_multi_remove_handle($this->curl_multi_handle, $this->curl_handle[$key]);
584
      curl_close($this->curl_handle[$key]);
82190 hhl 585
    }
118290 fvs 586
    $this->curl_handle = array();
119243 fvs 587
        $this->curl_content = null;
588
        $this->curl_status = null;
87714 hhl 589
// keep the multihandle in order to reuse sockets
118290 fvs 590
    //curl_multi_close($this->curl_multi_handle);
591
    //$this->curl_multi_handle = null ;
592
  }
82190 hhl 593
 
118290 fvs 594
  /////////////////////
595
  // PRIVATE METHODS //
596
  /////////////////////
82190 hhl 597
 
118290 fvs 598
  /**
599
   * Check if there's a handle for the handle number, and if not, create the handle
600
   * and assign default values.
601
   * @param $handle_no - Handle number. (integer)
602
   * @return void
603
   */
604
  private function handle_check($handle_no) {
605
    if (!isset($this->curl_handle[$handle_no])) {
606
      $this->curl_handle[$handle_no] = curl_init();
607
      foreach ($this->curl_default_options as $option => $option_value) {
608
        self::set_option($option, $option_value, $handle_no);
609
      }
82190 hhl 610
    }
118290 fvs 611
  }
82190 hhl 612
 
118290 fvs 613
  /**
614
   * Callback function to catch header-lines and store cookies for later use
615
   *
616
   * @param $ch (ressource)       - cURL handle
617
   * @param $header_line (string) - a header line
618
   *
619
   * @return string  - the callback function has to return the length of the line
620
   */
621
  private function callback_save_cookies($ch, $header_line) {
622
    if (substr($header_line, 0, 11) == 'Set-Cookie:') {
623
      $parts = explode(';', substr($header_line, 11));
624
      $cookie = trim($parts[0]);
625
      if (empty($this->cookies[$ch]) || !in_array($cookie, $this->cookies[$ch])) {
626
        $this->cookies[$ch][] = $cookie;
627
      }
82190 hhl 628
    }
118290 fvs 629
    return strlen($header_line);
630
  }
82190 hhl 631
 
120114 fvs 632
  /** \brief -
633
   * @param string $level
634
   * @param string $msg
635
   */
636
  private function local_verbose($level, $msg) {
637
    if (method_exists('VerboseJson', 'log')) {
638
      VerboseJson::log($level, $msg);
639
    }
640
    elseif (method_exists('verbose', 'log')) {
641
      verbose::log($level, $msg);
642
    }
643
    elseif (function_exists('verbose')) {
644
      verbose($level, $msg);
645
    }
646
  }
647
 
82190 hhl 648
}