Development

Changeset 4697 for plugins/sfWebBrowserPlugin/lib/sfCurlAdapter.class.php

You must first sign up to be able to contribute.

Show
Ignore:
Timestamp:
07/21/07 01:29:14 (2 years ago)
Author:
bmeynell
Message:

bmeynell: made curl adapter faster and added cookie support, fixed small bug with getResponseBody() in sfWebBrowser

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfWebBrowserPlugin/lib/sfCurlAdapter.class.php

    r4035 r4697  
    2424  protected 
    2525    $options = array(), 
     26    $curl    = null, 
    2627    $headers = array(); 
    2728 
    2829  public function __construct($options = array()) 
    2930  { 
    30     if (!function_exists('curl_init')) 
     31    if (!extension_loaded('curl')) 
    3132    { 
    32       throw new Exception('Curl not enabled'); 
     33      throw new Exception('Curl extension not loaded'); 
    3334    } 
    3435 
    3536    $this->options = $options; 
     37    $this->curl = curl_init(); 
     38 
     39    // cookies 
     40    if (isset($this->options['cookies'])) 
     41    { 
     42      $cookie_file = isset($this->options['cookies_file']) ? $this->options['cookies_file'] : sfConfig::get('sf_data_dir').'/sfWebBrowserPlugin/sfCurlAdapter/cookies.txt'; 
     43      $cookie_dir = isset($this->options['cookies_dir']) ? $this->options['cookies_dir'] : sfConfig::get('sf_data_dir').'/sfWebBrowserPlugin/sfCurlAdapter'; 
     44 
     45      if (!is_dir($cookie_dir)) 
     46      { 
     47        if (!mkdir($cookie_dir, 0777, true)) 
     48        { 
     49          throw new Exception(sprintf('Could not create directory "%s"', $cookie_dir)); 
     50        } 
     51      } 
     52 
     53      curl_setopt($this->curl, CURLOPT_COOKIESESSION, false); 
     54      curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookie_file); 
     55      curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookie_file); 
     56    } 
     57 
     58    // default settings 
     59    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); 
     60    curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); 
     61    curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); 
     62    curl_setopt($this->curl, CURLOPT_FRESH_CONNECT, true); 
     63 
     64    if (isset($this->options['verbose'])) 
     65    { 
     66      curl_setopt($this->curl, CURLOPT_NOPROGRESS, false); 
     67      curl_setopt($this->curl, CURLOPT_VERBOSE, true); 
     68    } 
     69 
     70    if (isset($this->options['verbose_log'])) 
     71    { 
     72      $log_file = sfConfig::get('sf_log_dir').'/sfCurlAdapter_verbose.log'; 
     73      curl_setopt($this->curl, CURLOPT_VERBOSE, true); 
     74      $this->fh = fopen($log_file, 'a+b'); 
     75      curl_setopt($this->curl, CURLOPT_STDERR, $this->fh); 
     76    } 
     77 
     78    // response header storage - uses callback function 
     79    curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array($this, 'read_header')); 
    3680  } 
    3781 
     
    4589   * 
    4690   * @return sfWebBrowser The current browser object 
    47    */   
     91   */ 
    4892  public function call($browser, $uri, $method = 'GET', $parameters = array(), $headers = array()) 
    4993  { 
    50     $curl = curl_init(); 
    51      
    52     // default settings  
    53     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    54     curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    55     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
    56  
    5794    // uri 
    58     curl_setopt($curl, CURLOPT_URL, $uri); 
     95    curl_setopt($this->curl, CURLOPT_URL, $uri); 
    5996 
    6097    // request headers 
    6198    $m_headers = array_merge($browser->getDefaultRequestHeaders(), $browser->initializeRequestHeaders($headers)); 
    6299    $request_headers = explode("\r\n", $browser->prepareHeaders($m_headers)); 
    63     curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers); 
     100    curl_setopt($this->curl, CURLOPT_HTTPHEADER, $request_headers); 
    64101 
    65102    // encoding support 
    66     isset($headers['Accept-Encoding']) ? curl_setopt($curl, CURLOPT_ENCODING, $headers['Accept-Encoding']) : null; 
     103    isset($headers['Accept-Encoding']) ? curl_setopt($this->curl, CURLOPT_ENCODING, $headers['Accept-Encoding']) : null; 
    67104 
    68     // store response headers, uses callback function 
    69     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, 'read_header')); 
     105    // handle any request method 
     106    curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); 
    70107 
    71108    if (!empty($parameters)) 
    72109    { 
    73       curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameters)); 
     110      curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($parameters, '', '&')); 
    74111    } 
    75112 
    76     // handle any request method 
    77     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 
     113    $response = curl_exec($this->curl); 
    78114 
    79     $response = curl_exec($curl); 
    80  
    81     if (curl_errno($curl)) 
     115    if (curl_errno($this->curl)) 
    82116    { 
    83       throw new Exception(curl_error($curl)); 
     117      throw new Exception(curl_error($this->curl)); 
    84118    } 
    85119 
    86     $requestInfo = curl_getinfo($curl); 
     120    $requestInfo = curl_getinfo($this->curl); 
    87121 
    88122    $browser->setResponseCode($requestInfo['http_code']); 
     
    93127    $this->headers = array(); 
    94128 
    95     curl_close($curl); 
    96  
    97129    return $browser; 
    98130  } 
    99131 
    100   private function read_header($curl, $headers) 
     132  public function __destroy() 
     133  { 
     134    curl_close($this->curl); 
     135  } 
     136 
     137  protected function read_header($curl, $headers) 
    101138  { 
    102139    $this->headers[] = $headers; 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.