Subversion Repositories Web Services

Rev

Rev 118290 | Go to most recent revision | 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')) {
163
      if (method_exists('verbose', 'log')) {
164
        verbose::log(FATAL, "PHP was not built with curl, rebuild PHP to use the curl class.");
165
      }
166
      elseif (function_exists('verbose')) {
167
        verbose(FATAL, "PHP was not built with curl, rebuild PHP to use the curl class.");
168
      }
169
      return FALSE;
170
    }
82190 hhl 171
 
118290 fvs 172
    if (!isset($curl_default_options)) {
173
      if (method_exists('verbose', 'log')) {
174
        verbose::log(ERROR, '$curl_default_options is not defined. See the class description for usage');
175
      }
176
      elseif (function_exists('verbose')) {
177
        verbose(ERROR, '$curl_default_options is not defined. See the class description for usage');
178
      }
179
      return FALSE;
180
    }
181
    else
182
      $this->curl_default_options = $curl_default_options;
82190 hhl 183
 
118290 fvs 184
    $this->curl_handle[] = curl_init();
82190 hhl 185
 
118290 fvs 186
    $this->set_multiple_options($this->curl_default_options);
187
    return TRUE;
188
  }
189
 
190
  /**
191
   * Set multiple options for a cURL transfer
192
   *
193
   * @link http://dk2.php.net/curl_setopt_array @endlink
194
   * @param $options - The array of curl options. See $curl_default_options (array)
195
   * @return bool  Returns TRUE if all options were successfully set (on all handles).
196
   *               If an option could not be successfully set, FALSE is immediately returned,
197
   *               ignoring any future options in the options array.
198
   */
199
  public function set_multiple_options($options = NULL) {
200
 
201
    if (!$options)
202
      return FALSE;
203
 
204
    foreach ($this->curl_handle as $key => $handle) {
205
      $res = curl_setopt_array($this->curl_handle[$key], $options);
206
      if (!$res)
207
        return FALSE;
82190 hhl 208
    }
118290 fvs 209
    reset($this->curl_handle);
210
    foreach ($this->curl_handle as $key => $handle) {
211
      foreach ($options as $option => $value) {
212
        $this->curl_options[$key][$option] = $value;
213
      }
214
    }
215
    return TRUE;
216
  }
82190 hhl 217
 
118290 fvs 218
  /**
219
   * Execute the curl request and return the result.
220
   *
221
   * @link http://www.php.net/curl_multi_close @endlink \n
222
   * @link http://www.php.net/curl_multi_init @endlink \n
223
   * @link http://www.php.net/curl_multi_add_handle @endlink \n
224
   * @link http://www.php.net/curl_multi_exec @endlink \n
225
   * @link http://www.php.net/curl_multi_getcontent @endlink \n
226
   * @link http://www.php.net/curl_getinfo @endlink \n
227
   * @link http://www.php.net/curl_errno @endlink \n
228
   * @link http://www.php.net/curl_error @endlink \n
229
   * @param boolean $urls
230
   * @return string The contents of the page (or other interaction as defined by the
231
   *                settings of the various curl options).
232
   */
233
  public function get($urls = FALSE) {
82190 hhl 234
 
118290 fvs 235
    if ($urls)
236
      self::set_url($urls);
82190 hhl 237
 
118290 fvs 238
    // remove previous curl_multi_handle, if any
239
    if (is_resource($this->curl_multi_handle)) {
240
      if (is_array($this->curl_handle)) {
87714 hhl 241
        foreach ($this->curl_handle as $key => $handle) {
118290 fvs 242
          curl_multi_remove_handle($this->curl_multi_handle, $this->curl_handle[$key]);
87714 hhl 243
        }
118290 fvs 244
      }
245
    }
246
    else {
247
      //create a new multiple cURL handle
248
      $this->curl_multi_handle = curl_multi_init();
249
    }
250
 
251
    // set cookies and add the handles
252
    foreach ($this->curl_handle as $key => $handle) {
253
      if (array_key_exists(intval($handle), $this->cookies)) {
254
        if ($this->cookies[intval($handle)]) {
255
          self::set_option(CURLOPT_COOKIE, implode(';', $this->cookies[$handle]));
87714 hhl 256
        }
118290 fvs 257
      }
258
      curl_multi_add_handle($this->curl_multi_handle, $this->curl_handle[$key]);
87714 hhl 259
    }
82190 hhl 260
 
118290 fvs 261
    // execute the handles
262
    do {
263
      /*
264
        curl_multi_select() should according to the manual:
265
        'Blocks until there is activity on any of the curl_multi connections.'
266
        but including the line below without an timeout, more than doubles the time used in this function???
82190 hhl 267
 
118290 fvs 268
        Has to call it with a timeout less than 1, or it will apparently default (and wait) 1 second for
269
        each connection????????
270
       */
271
      curl_multi_select($this->curl_multi_handle, 0.01);
272
      $status = curl_multi_exec($this->curl_multi_handle, $active);
273
      if ($info = curl_multi_info_read($this->curl_multi_handle)) {
274
        $multi_status[intval($info['handle'])] = $info['result'];
275
        if (curl_getinfo($info['handle'], CURLINFO_HTTP_CODE) == 200)
276
          $this->wait_for_connections--;
277
      }
278
    } while ($this->wait_for_connections && ($status === CURLM_CALL_MULTI_PERFORM || $active));
279
    if ($info = curl_multi_info_read($this->curl_multi_handle)) {
280
      $multi_status[intval($info['handle'])] = $info['result'];
281
    }
282
    foreach ($this->curl_handle as $key => $handle) {
283
      $this->curl_status[$key] = curl_getinfo($this->curl_handle[$key]);
284
      $this->curl_status[$key]['errno'] = curl_errno($this->curl_handle[$key]);
285
      $this->curl_status[$key]['error'] = curl_error($this->curl_handle[$key]);
286
      if (empty($this->curl_status[$key]['errno']) && isset($multi_status[$handle])) {
287
        $this->curl_status[$key]['errno'] = $multi_status[intval($handle)];
288
      }
289
      if ($this->curl_status[$key]['errno'] == 28) { // CURLE_OPERATION_TIMEDOUT
290
        $this->curl_status[$key]['http_code'] = 504;  // Gateway timeout
291
      }
292
    }
293
    foreach ($this->curl_handle as $key => $handle) {
294
      $this->curl_content[$key] = curl_multi_getcontent($handle);
295
      if (method_exists('curl_recorder', 'record')) {
296
        $curl_recorder->record('status', $this->curl_status[$key], $key);
297
        $curl_recorder->record('result', $this->curl_content[$key], $key);
298
      }
299
    }
82190 hhl 300
 
118290 fvs 301
    if (sizeof($this->curl_handle) == 1)
302
      return $this->curl_content[0];
303
    else
304
      return $this->curl_content;
305
  }
82190 hhl 306
 
118290 fvs 307
  /**
308
   * Returns the current setting of the request option.
309
   * If no handle_number has been set, it return the settings of all handles.
310
   *
311
   * @param $option - One of the valid CURLOPT defines. (mixed)
312
   * @param $handle_no - Handle number. (integer)
313
   * @returns mixed
314
   */
315
  public function get_option($option = null, $handle_no = 0) {
82190 hhl 316
 
118290 fvs 317
    foreach ($this->curl_handle as $key => $handle) {
318
      if (!$handle_no || $key == $handle_no) {
319
        if (empty($option)) {
320
          $option_values[] = $this->curl_options[$key];
87714 hhl 321
        }
118290 fvs 322
        else {
323
          if (isset($this->curl_options[$key][$option]))
324
            $option_values[] = $this->curl_options[$key][$option];
325
          else
326
            $option_values[] = null;
87714 hhl 327
        }
118290 fvs 328
      }
82190 hhl 329
    }
330
 
118290 fvs 331
    if ($handle_no || sizeof($this->curl_handle) == 1)
332
      return $option_values[0];
333
    else
334
      return $option_values;
335
  }
82190 hhl 336
 
118290 fvs 337
  /**
338
   * Set a curl option.
339
   *
340
   * @link http://www.php.net/curl_setopt @endlink
341
   * @param $option - One of the valid CURLOPT defines. (mixed)
342
   * @param $value - The value of the curl option. (mixed)
343
   * @param $handle_no - Handle number. (integer)
344
   * @return boolean
345
   */
346
  public function set_option($option, $value, $handle_no = null) {
82190 hhl 347
 
118290 fvs 348
    if ($handle_no === null) {
349
      foreach ($this->curl_handle as $key => $handle) {
350
        $this->curl_options[$key][$option] = $value;
351
        $res = curl_setopt($this->curl_handle[$key], $option, $value);
352
        if (!$res)
353
          return FALSE;
354
      }
82190 hhl 355
    }
118290 fvs 356
    else {
357
      self::handle_check($handle_no);
358
      $this->curl_options[$handle_no][$option] = $value;
359
      $res = curl_setopt($this->curl_handle[$handle_no], $option, $value);
82190 hhl 360
    }
118290 fvs 361
    return $res;
362
  }
82190 hhl 363
 
118290 fvs 364
  /**
365
   * Set CURLOPT_URL value(s).
366
   * @param $value (s)   - The value of the curl option. (mixed)
367
   * @param $handle_no - Handle number. Default 0. (integer)
368
   * @return void
369
   */
370
  public function set_url($value, $handle_no = 0) {
371
    if (is_array($value)) {
372
      foreach ($value as $key => $url) {
373
        self::set_option(CURLOPT_URL, $url, $key);
374
        if (method_exists('curl_recorder', 'record_url')) {
375
          $curl_recorder->record_url($url, $key);
87714 hhl 376
        }
118290 fvs 377
      }
87714 hhl 378
    }
118290 fvs 379
    else {
380
      self::set_option(CURLOPT_URL, $value, $handle_no);
381
      if (method_exists('curl_recorder', 'record_url')) {
382
        $curl_recorder->record_url($value, $handle_no);
383
      }
87714 hhl 384
    }
118290 fvs 385
  }
82190 hhl 386
 
118290 fvs 387
  /**
388
   * Set HTTP proxy value(s).
389
   * @param $value - HTTP proxy
390
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
391
   * @return boolean
392
   */
393
  public function set_proxy($value, $handle_no = null) {
394
    if ($ret = self::set_option(CURLOPT_HTTPPROXYTUNNEL, TRUE, $handle_no))
395
      $ret = self::set_option(CURLOPT_PROXY, $value, $handle_no);
396
    return $ret;
397
  }
82190 hhl 398
 
118290 fvs 399
  /**
400
   * Set using cookies
401
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
402
   * @return boolean
403
   */
404
  public function use_cookies($handle_no = null) {
405
    return self::set_option(CURLOPT_HEADERFUNCTION, array($this, 'callback_save_cookies'), $handle_no);
406
  }
82190 hhl 407
 
118290 fvs 408
  /**
409
   * Set HTTP authentication value(s).
410
   * @param $user - HTTP user
411
   * @param $passwd - HTTP password
412
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
413
   * @return boolean
414
   */
415
  public function set_authentication($user, $passwd, $handle_no = null) {
416
    return self::set_option(CURLOPT_USERPWD, $user . ':' . $passwd, $handle_no);
417
  }
82190 hhl 418
 
118290 fvs 419
  /**
420
   * Set HTTP proxy authentication value(s).
421
   * @param $user - HTTP proxy user
422
   * @param $passwd - HTTP proxy password
423
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
424
   * @return boolean
425
   */
426
  public function set_proxy_authentication($user, $passwd, $handle_no = null) {
427
    return self::set_option(CURLOPT_PROXYUSERPWD, '[' . $user . ']:[' . $passwd . ']', $handle_no);
428
  }
82190 hhl 429
 
118290 fvs 430
  /**
431
   * Set timeout
432
   * @param $seconds - timeout ind seconds
433
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
434
   * @return boolean
435
   */
436
  public function set_timeout($seconds, $handle_no = null) {
437
    return self::set_option(CURLOPT_TIMEOUT, $seconds, $handle_no);
438
  }
82190 hhl 439
 
118290 fvs 440
  /**
441
   * Set number of connections to wait for
442
   * @param $wait_for_connections - max connections to wait for
443
   * @return boolean
444
   */
445
  public function set_wait_for_connections($wait_for_connections) {
446
    $this->wait_for_connections = $wait_for_connections;
447
    return TRUE;
448
  }
87714 hhl 449
 
118290 fvs 450
  /**
451
   * Set POST value(s).
452
   * @param $value - The value(s) to post
453
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
454
   * @return boolean
455
   */
456
  public function set_post($value, $handle_no = null) {
457
    if ($ret = self::set_option(CURLOPT_POST, 1, $handle_no))
458
      $ret = self::set_option(CURLOPT_POSTFIELDS, $value, $handle_no);
459
    if (method_exists('curl_recorder', 'record_parameters')) {
460
      $curl_recorder->record_parameters($value, $handle_no);
82190 hhl 461
    }
118290 fvs 462
    return $ret;
463
  }
82190 hhl 464
 
118290 fvs 465
  /**
466
   * Set POST value(s) with Content-Type: text/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_xml($value, $handle_no = null) {
472
    return self::set_post_with_header($value, 'Content-Type: text/xml', $handle_no);
473
  }
82190 hhl 474
 
118290 fvs 475
  /**
476
   * Set POST value(s) with Content-Type: application/xml.
477
   * @param $value - The value(s) to post
478
   * @param $handle_no - Handle number. Default all handle numbers. (integer)
479
   * @return boolean
480
   */
481
  public function set_post_application_xml($value, $handle_no = null) {
482
    return self::set_post_with_header($value, 'Content-Type: application/xml', $handle_no);
483
  }
82190 hhl 484
 
118290 fvs 485
  /**
486
   * Set POST value(s).
487
   * @param $header_line - Like 'Content-Type: ...'
488
   * @param string $value - The value(s) to post
489
   * @param string $header_line - Like 'Content-Type: ...'
490
   * @param integer $handle_no - Handle number. Default all handle numbers. (integer)
491
   * @return boolean
492
   */
493
  public function set_post_with_header($value, $header_line, $handle_no = null) {
494
    $headers = $this->get_option(CURLOPT_HTTPHEADER, $handle_no);
495
    $headers[] = $header_line;
496
    if ($ret = self::set_option(CURLOPT_HTTPHEADER, $headers, $handle_no))
497
      $ret = self::set_post($value, $handle_no);
498
    return $ret;
499
  }
500
 
501
  /**
502
   * Set SOAP Action
503
   * @param string $action - The soap-action
504
   * @param integer $handle_no - Handle number. Default all handle numbers. (integer)
505
   * @return boolean
506
   */
507
  public function set_soap_action($action, $handle_no = null) {
508
    $headers = $this->get_option(CURLOPT_HTTPHEADER, $handle_no);
509
    $headers[] = "SOAPAction: " . $action;
510
    return self::set_option(CURLOPT_HTTPHEADER, $headers, $handle_no);
511
  }
512
 
513
  /**
514
   * Get next available handle ID.
515
   * @return integer
516
   */
517
  public function get_next_handle() {
518
    $next_handle_no = 0;
519
    foreach ($this->curl_handle as $key => $handle) {
520
      if ($key > $next_handle_no)
521
        $next_handle_no = $key;
82190 hhl 522
    }
118290 fvs 523
    return $next_handle_no + 1;
524
  }
82190 hhl 525
 
118290 fvs 526
  /**
527
   * Return the status information of the last curl request.
528
   *
529
   * @param string $field [optional] the particular portion (string)
530
   *                     of the status information desired.
531
   *                     If omitted the array of status
532
   *                     information is returned.  If a non-existent
533
   *                     status field is requested, FALSE is returned.
534
   * @param integer $handle_no  Handle number. (integer)
535
   * @return mixed
536
   */
537
  public function get_status($field = null, $handle_no = 0) {
538
 
539
    foreach ($this->curl_handle as $key => $handle) {
540
      if (!$handle_no || $key == $handle_no) {
541
        if (empty($field)) {
542
          $status[] = $this->curl_status[$key];
82190 hhl 543
        }
118290 fvs 544
        else {
545
          if (isset($this->curl_status[$key][$field])) {
546
            $status[] = $this->curl_status[$key][$field];
547
          }
548
          else
549
            return FALSE;
550
        }
551
      }
82190 hhl 552
    }
553
 
118290 fvs 554
    if ($handle_no || sizeof($this->curl_handle) == 1)
555
      return $status[0];
556
    else
557
      return $status;
558
  }
82190 hhl 559
 
118290 fvs 560
  /**
561
   * Did the last curl exec operation have an error?
562
   *
563
   * @param $handle_no - Handle number. (integer)
564
   * @return mixed  The error message associated with the error if an error
565
   *                occurred, FALSE otherwise.
566
   */
567
  public function has_error($handle_no = 0) {
568
 
569
    foreach ($this->curl_handle as $key => $handle) {
570
      if (!$handle_no || $key == $handle_no) {
571
        if (isset($this->curl_status[$key]['error'])) {
572
          $has_error[] = (empty($this->curl_status[$key]['error']) ? FALSE : $this->curl_status[$key]['error']);
87714 hhl 573
        }
574
        else
118290 fvs 575
          $has_error[] = FALSE;
576
      }
87714 hhl 577
    }
82190 hhl 578
 
118290 fvs 579
    if ($handle_no || sizeof($this->curl_handle) == 1)
580
      return $has_error[0];
581
    else
582
      return $has_error;
583
  }
82190 hhl 584
 
118290 fvs 585
  /**
586
   * Free the resources associated with the curl session.
587
   *
588
   * @link http://www.php.net/curl_close @endlink
589
   * $return void
590
   */
591
  public function close() {
592
    foreach ($this->curl_handle as $key => $handle) {
593
      curl_multi_remove_handle($this->curl_multi_handle, $this->curl_handle[$key]);
594
      curl_close($this->curl_handle[$key]);
82190 hhl 595
    }
118290 fvs 596
    $this->curl_handle = array();
119243 fvs 597
        $this->curl_content = null;
598
        $this->curl_status = null;
87714 hhl 599
// keep the multihandle in order to reuse sockets
118290 fvs 600
    //curl_multi_close($this->curl_multi_handle);
601
    //$this->curl_multi_handle = null ;
602
  }
82190 hhl 603
 
118290 fvs 604
  /////////////////////
605
  // PRIVATE METHODS //
606
  /////////////////////
82190 hhl 607
 
118290 fvs 608
  /**
609
   * Check if there's a handle for the handle number, and if not, create the handle
610
   * and assign default values.
611
   * @param $handle_no - Handle number. (integer)
612
   * @return void
613
   */
614
  private function handle_check($handle_no) {
615
    if (!isset($this->curl_handle[$handle_no])) {
616
      $this->curl_handle[$handle_no] = curl_init();
617
      foreach ($this->curl_default_options as $option => $option_value) {
618
        self::set_option($option, $option_value, $handle_no);
619
      }
82190 hhl 620
    }
118290 fvs 621
  }
82190 hhl 622
 
118290 fvs 623
  /**
624
   * Callback function to catch header-lines and store cookies for later use
625
   *
626
   * @param $ch (ressource)       - cURL handle
627
   * @param $header_line (string) - a header line
628
   *
629
   * @return string  - the callback function has to return the length of the line
630
   */
631
  private function callback_save_cookies($ch, $header_line) {
632
    if (substr($header_line, 0, 11) == 'Set-Cookie:') {
633
      $parts = explode(';', substr($header_line, 11));
634
      $cookie = trim($parts[0]);
635
      if (empty($this->cookies[$ch]) || !in_array($cookie, $this->cookies[$ch])) {
636
        $this->cookies[$ch][] = $cookie;
637
      }
82190 hhl 638
    }
118290 fvs 639
    return strlen($header_line);
640
  }
82190 hhl 641
 
642
}