curl.php.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 800 行 · 第 1/2 页
SVN-BASE
800 行
return $result; } } // Map the deprecated variables and throw a bunch of errors $this->_mapDeprecatedVariables(); // Default return value is true. $ret = true; // Basic stuff $ret = curl_setopt($this->_ch, CURLOPT_URL, $this->url); $ret = curl_setopt($this->_ch, CURLOPT_HEADER, $this->header); // Whether or not to return the transfer contents if ($this->returnTransfer === true) { $ret = curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true); } // HTTP Authentication if ($this->username != '') { $ret = curl_setopt($this->_ch, CURLOPT_USERPWD, $this->username . ':' . $this->password); } // SSL Checks if (isset($this->sslVersion)) { $ret = curl_setopt($this->_ch, CURLOPT_SSLVERSION, $this->sslVersion); } if (isset($this->sslCert)) { $ret = curl_setopt($this->_ch, CURLOPT_SSLCERT, $this->sslCert); } if (isset($this->sslCertPasswd)) { $ret = curl_setopt($this->_ch, CURLOPT_SSLCERTPASSWD, $this->sslCertPasswd); } // Proxy Related checks if (isset($this->proxy)) { $ret = curl_setopt($this->_ch, CURLOPT_PROXY, $this->proxy); } if (isset($this->proxyUser) || isset($this->proxyPassword)) { $ret = curl_setopt($this->_ch, CURLOPT_PROXYUSERPWD, $this->proxyUser . ':' . $this->proxyPassword); } if (is_bool($this->verifyPeer)) { if(!$this->setOption(CURLOPT_SSL_VERIFYPEER,$this->verifyPeer)) { return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER'); } } if (is_numeric($this->verifyHost) && $this->verifyHost >= 0 && $this->verifyHost <= 2) { if(!$this->setOption(CURLOPT_SSL_VERIFYHOST,$this->verifyHost)) { return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER'); } } if (is_bool($this->verifyPeer) && $this->verifyPeer == true) { if (isset($this->caInfo) && strlen($this->caInfo)) { if (file_exists($this->caInfo)) { if (!$this->setOption(CURLOPT_CAINFO,$this->caInfo)) { return PEAR::raiseError('Error setting CURLOPT_CAINFO'); } } else { return PEAR::raiseError('Could not find CA info: '.$this->caInfo); } } if (isset($this->caPath) && is_string($this->caPath)) { if (!$this->setOption(CURLOPT_CAPATH,$this->caPath)) { return PEAR::raiseError('Error setting CURLOPT_CAPATH'); } } } // Transfer type if (isset($this->type)) { switch (strtolower($this->type)) { case 'post': $ret = curl_setopt($this->_ch, CURLOPT_POST, true); break; case 'put': $ret = curl_setopt($this->_ch, CURLOPT_PUT, true); break; } } // Transfer upload, etc. related if (isset($this->file)) { if (!file_exists($this->file)) { return PEAR::raiseError('File does not exist: '.$this->file); } $this->_fp = fopen($this->file,'r'); if (!is_resource($this->_fp)) { return PEAR::raiseError('Could not open file: '.$this->file); } if (!isset($this->fileSize)) { $this->fileSize = filesize($this->file); } $ret = curl_setopt($this->_ch, CURLOPT_INFILE, $this->_fp); $ret = curl_setopt($this->_ch, CURLOPT_INFILESIZE, $this->fileSize); $ret = curl_setopt($this->_ch, CURLOPT_UPLOAD, true); } if (isset($this->fields)) { if (!isset($this->type)) { $this->type = 'post'; $ret = curl_setopt($this->_ch, CURLOPT_POST, true); } // If fields is an array then turn it into a string. Somtimes // cURL doesn't like fields as an array. if (is_array($this->fields)) { $sets = array(); foreach ($this->fields as $key => $val) { $sets[] = $key . '=' . urlencode($val); } $fields = implode('&',$sets); } else { $fields = $this->fields; } $ret = curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $fields); } // Error related if ($this->progress === true) { $ret = curl_setopt($this->_ch, CURLOPT_PROGRESS, true); } if ($this->verbose === true) { $ret = curl_setopt($this->_ch, CURLOPT_VERBOSE, true); } if ($this->mute !== true) { $ret = curl_setopt($this->_ch, CURLOPT_MUTE, false); } // If a Location: header is passed then follow it $ret = curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, $this->followLocation); // If a timeout is set and is greater then zero then set it if (is_numeric($this->timeout) && $this->timeout > 0) { $ret = curl_setopt($this->_ch, CURLOPT_TIMEOUT, $this->timeout); } if (isset($this->userAgent)) { $ret = curl_setopt($this->_ch, CURLOPT_USERAGENT, $this->userAgent); } // Cookies if (is_array($this->cookies) && count($this->cookies)) { $cookieData = ''; foreach ($this->cookies as $name => $value) { $cookieData = $name . '=' . $value . ';'; } $ret = curl_setopt($this->_ch, CURLOPT_COOKIE, $cookieData); } // Other HTTP headers if ($this->httpHeaders !== null) { if (is_array($this->httpHeaders)) { $ret = curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $this->httpHeaders); } else { return PEAR::raiseError('Net_Curl::$httpHeaders must be an array'); } } $ret = curl_exec($this->_ch); // Close the file before we return anything if (is_resource($this->_fp)) { fclose($this->_fp); } if (curl_errno($this->_ch)) { return PEAR::raiseError(curl_error($this->_ch), curl_errno($this->_ch)); } // Check to make sure we get a 2XX/3XX code and not a 404 or something. $info = $this->getInfo(); if (!isset($info['http_code'])) { return PEAR::raiseError('Unknown or invalid HTTP response'); } else { $type = substr($info['http_code'],0,1); if ($type != 2 && $type != 3) { return PEAR::raiseError('Unexpected HTTP code: '.$info['http_code']); } } return $ret; } // }}} // {{{ setOption($option, $value) /** * setOption * * Set a option for your cURL session. Please note that the cURL handler * is NOT created before execute(). This is for error checking purposes. * You should use setOption() in the following manner: * * <code> * <?php * * require_once('Net/Curl.php'); * $curl = & new Net_Curl('http://www.example.com'); * $check = $curl->create(); * if (!PEAR::isError($check)) { * $curl->setOption(CURLOPT_FOO,'bar'); * $result = $curl->execute(); * if (!PEAR::isError($result)) { * echo $result; * } * } * * ?> * </code> * * @author Joe Stump <joe@joestump.net> * @access public * @param int $option cURL constant (ie. CURLOPT_URL) * @param mixed $value * @return boolean */ function setOption($option, $value) { if (is_resource($this->_ch)) { return curl_setopt($this->_ch, $option, $value); } return false; } // }}} // {{{ getInfo() /** * getInfo * * Returns the info from the cURL session. PEAR_Error if you try and run * this before you execute the session. * * @author Joe Stump <joe@joestump.net> * @access public * @return mixed PEAR_Error if there is no resource, info on success */ function getInfo() { if (is_resource($this->_ch)) { return curl_getinfo($this->_ch); } return PEAR::isError('cURL handler does not exist!'); } // }}} // {{{ create() /** * create * * Create a cURL resource. If curl_init() doesn't exist or we could not * create a resource it will error out. * * @author Joe Stump <joe@joestump.net> * @return mixed PEAR_Error on failure, true on success */ function create() { if (!function_exists('curl_init')) { return PEAR::raiseError('Function curl_init() not found'); } $this->_ch = curl_init(); if (!is_resource($this->_ch)) { return PEAR::raiseError('Could not initialize cURL handler'); } return true; } // }}} // {{{ verboseAll() /** * Set a verbose output * * Turns on super debugging mode by not suppressing errors, turning on * verbose mode, showing headers and displaying progress. * * @access public * @author David Costa <gurugeek@php.net> * @return void */ function verboseAll() { $this->verbose = true; $this->mute = false; $this->header = true; $this->progress = true; } // }}} // {{{ verbose_all() /** * Set a verbose output * * @access public * @author David Costa <gurugeek@php.net> * @deprecated */ function verbose_all() { $this->verboseAll(); PEAR::raiseError('Net_Curl::verbose_all() is deprecated! Please use Net_Curl::verboseAll()'." <br />\n",null,PEAR_ERROR_PRINT); } // }}} // {{{ close() /** * Closes the curl transfer and finishes the object (kinda ;) * * @access public * @author Sterling Hughes <sterling@php.net> * @return void * @since PHP 4.0.5 */ function close() { if (is_resource($this->_ch)) { curl_close($this->_ch); } } // }}} // {{{ _mapDeprecatedVariables() /** * _mapDeprecatedVariables * * Maps deprecated variables into the appropriate places. It also throws * the necessary notices. * * @author Joe Stump <joe@joestump.net> * @access private * @return void */ function _mapDeprecatedVariables() { $bad = array(); if ($this->follow_location !== false) { if ($this->follow_location > 0) { $this->followLocation = true; } else { $this->followLocation = false; } $bad[] = array('follow_location','followLocation'); } if ($this->return_transfer !== false) { if ($this->return_transfer > 0) { $this->returnTransfer = true; } else { $this->returnTransfer = false; } $bad[] = array('return_transfer','returnTransfer'); } if ($this->file_size !== false) { $this->fileSize = $this->file_size; $bad[] = array('file_size','fileSize'); } if ($this->http_headers !== false) { $this->httpHeaders = $this->http_headers; $bad[] = array('http_headers','httpHeaders'); } foreach ($bad as $map) { PEAR::raiseError('Net_Curl::$'.$map[0].' is deprecated! Please use Net_Curl::$'.$map[1]." instead! <br />\n",null,PEAR_ERROR_PRINT); } } // {{{ __destruct() /** * __destruct * * PHP 5.x destructor. Runs Net_Curl::close() to make sure we close our * cURL connection. * * @author Joe Stump <joe@joestump.net> * @see Net_Curl::close() */ function __destruct() { $this->close(); } // }}}}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?