Changeset 4697 for plugins/sfWebBrowserPlugin/lib/sfCurlAdapter.class.php
- Timestamp:
- 07/21/07 01:29:14 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfWebBrowserPlugin/lib/sfCurlAdapter.class.php
r4035 r4697 24 24 protected 25 25 $options = array(), 26 $curl = null, 26 27 $headers = array(); 27 28 28 29 public function __construct($options = array()) 29 30 { 30 if (! function_exists('curl_init'))31 if (!extension_loaded('curl')) 31 32 { 32 throw new Exception('Curl not enabled');33 throw new Exception('Curl extension not loaded'); 33 34 } 34 35 35 36 $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')); 36 80 } 37 81 … … 45 89 * 46 90 * @return sfWebBrowser The current browser object 47 */ 91 */ 48 92 public function call($browser, $uri, $method = 'GET', $parameters = array(), $headers = array()) 49 93 { 50 $curl = curl_init();51 52 // default settings53 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);54 curl_setopt($curl, CURLOPT_AUTOREFERER, true);55 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);56 57 94 // uri 58 curl_setopt($ curl, CURLOPT_URL, $uri);95 curl_setopt($this->curl, CURLOPT_URL, $uri); 59 96 60 97 // request headers 61 98 $m_headers = array_merge($browser->getDefaultRequestHeaders(), $browser->initializeRequestHeaders($headers)); 62 99 $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); 64 101 65 102 // 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; 67 104 68 // store response headers, uses callback function69 curl_setopt($ curl, CURLOPT_HEADERFUNCTION, array($this, 'read_header'));105 // handle any request method 106 curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); 70 107 71 108 if (!empty($parameters)) 72 109 { 73 curl_setopt($ curl, CURLOPT_POSTFIELDS, http_build_query($parameters));110 curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($parameters, '', '&')); 74 111 } 75 112 76 // handle any request method 77 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 113 $response = curl_exec($this->curl); 78 114 79 $response = curl_exec($curl); 80 81 if (curl_errno($curl)) 115 if (curl_errno($this->curl)) 82 116 { 83 throw new Exception(curl_error($ curl));117 throw new Exception(curl_error($this->curl)); 84 118 } 85 119 86 $requestInfo = curl_getinfo($ curl);120 $requestInfo = curl_getinfo($this->curl); 87 121 88 122 $browser->setResponseCode($requestInfo['http_code']); … … 93 127 $this->headers = array(); 94 128 95 curl_close($curl);96 97 129 return $browser; 98 130 } 99 131 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) 101 138 { 102 139 $this->headers[] = $headers;

