Source for file Curl.php
Documentation is available at Curl.php
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
* @package Microsoft_Http
* @subpackage Client_Adapter
* @version $Id: Curl.php 19238 2009-11-25 17:13:38Z bate $
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see Microsoft_AutoLoader
require_once dirname(__FILE__ ) . '/../../../AutoLoader.php';
* An adapter class for Microsoft_Http_Client based on the curl extension.
* Curl requires libcurl. See for full requirements the PHP manual: http://php.net/curl
* @package Microsoft_Http
* @subpackage Client_Adapter
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* What host/port are we connected to?
* The curl session handle
* List of cURL options that should never be overwritten
* Response gotten from server
* Stream for storing output
* Config is set using setConfig()
* @throws Microsoft_Http_Client_Adapter_Exception
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
* Set the configuration array for the adapter
* @throws Microsoft_Http_Client_Adapter_Exception
* @return Microsoft_Http_Client_Adapter_Curl
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
'Array expected, got ' . gettype($config)
if(isset ($config['proxy_user']) && isset ($config['proxy_pass'])) {
$this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user']. ":". $config['proxy_pass']);
unset ($config['proxy_user'], $config['proxy_pass']);
foreach ($config as $k => $v) {
* Retrieve the array of all configuration options
* Direct setter for cURL adapter related options.
* @param string|int$option
* @return Microsoft_Http_Adapter_Curl
if (!isset ($this->_config['curloptions'])) {
$this->_config['curloptions'] = array();
$this->_config['curloptions'][$option] = $value;
* @throws Microsoft_Http_Client_Adapter_Exception if unable to connect
public function connect($host, $port = 80, $secure = false)
// If we're already connected, disconnect first
// If we are connected to a different server or port, disconnect first
// Do the actual connection
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// Behave the same like Microsoft_Http_Adapter_Socket on SSL options.
if (isset ($this->_config['sslcert'])) {
if (isset ($this->_config['sslpassphrase'])) {
* Send request to the remote server
* @param Microsoft_Uri_Http $uri
* @return string $request
* @throws Microsoft_Http_Client_Adapter_Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '')
// Make sure we're properly connected
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// ensure correct curl call
$curlMethod = CURLOPT_HTTPGET;
$curlMethod = CURLOPT_POST;
// There are two different types of PUT request, either a Raw Data string has been set
// or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
$this->_config['curloptions'][CURLOPT_INFILE] = $body;
if (isset ($this->_config['curloptions'][CURLOPT_INFILE])) {
// Now we will probably already have Content-Length set, so that we have to delete it
// from $headers at this point:
foreach ($headers AS $k => $header) {
if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) {
$this->_config['curloptions'][CURLOPT_INFILESIZE] = (int) $m[1];
if (!isset ($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
$curlMethod = CURLOPT_PUT;
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlMethod = CURLOPT_CUSTOMREQUEST;
// For now, through an exception for unsupported request methods
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// get http version to use
$curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
// mark as HTTP request and set HTTP method
// headers will be read into the response
// and data will be written into the file
// ensure headers are also returned
// ensure actual response is returned
// set additional headers
* Make sure POSTFIELDS is set after $curlMethod is set:
* @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
} elseif ($curlMethod == CURLOPT_PUT) {
// this covers a PUT by file-handle:
// Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
// to group common functionality together.
unset ($this->_config['curloptions'][CURLOPT_INFILE]);
unset ($this->_config['curloptions'][CURLOPT_INFILESIZE]);
// This is a PUT by a setRawData string, not by file-handle
// set additional curl options
if (isset ($this->_config['curloptions'])) {
foreach ((array) $this->_config['curloptions'] as $k => $v) {
require_once 'Microsoft/Http/Client/Exception.php';
// if we used streaming, headers are already there
require_once 'Microsoft/Http/Client/Exception.php';
// cURL automatically decodes chunked-messages, this means we have to disallow the Microsoft_Http_Response to do it again
// Eliminate multiple HTTP responses.
if (isset ($parts[1]) && preg_match("|^HTTP/1\.[01](.*?)\r\n|mi", $parts[1])) {
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
* Return read response from server
* Close the connection to the server
* Set output stream for the response
* @param resource $stream
* @return Microsoft_Http_Client_Adapter_Socket
* Header reader function for CURL
|