1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Horde_Http_Request_Curl extends Horde_Http_Request_Base
18: {
19: 20: 21: 22: 23: 24:
25: protected $_httpAuthSchemes = array(
26: Horde_Http::AUTH_ANY => CURLAUTH_ANY,
27: Horde_Http::AUTH_BASIC => CURLAUTH_BASIC,
28: Horde_Http::AUTH_DIGEST => CURLAUTH_DIGEST,
29: Horde_Http::AUTH_GSSNEGOTIATE => CURLAUTH_GSSNEGOTIATE,
30: Horde_Http::AUTH_NTLM => CURLAUTH_NTLM,
31: );
32:
33: 34: 35: 36: 37:
38: public function __construct($args = array())
39: {
40: if (!extension_loaded('curl')) {
41: throw new Horde_Http_Exception('The curl extension is not installed. See http://php.net/curl.installation');
42: }
43:
44: parent::__construct($args);
45: }
46:
47: 48: 49: 50: 51: 52:
53: public function send()
54: {
55: $curl = curl_init();
56: curl_setopt($curl, CURLOPT_URL, $this->uri);
57: curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58: curl_setopt($curl, CURLOPT_HEADER, true);
59: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->method);
60: curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
61:
62:
63: curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
64:
65:
66: if ($this->redirects) {
67: curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
68: curl_setopt($curl, CURLOPT_MAXREDIRS, $this->redirects);
69: } else {
70: curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
71: curl_setopt($curl, CURLOPT_MAXREDIRS, 0);
72: }
73:
74: $data = $this->data;
75: if (is_array($data)) {
76:
77:
78:
79: $data = http_build_query($data);
80: }
81: if ($data) {
82: curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
83: }
84:
85:
86: if ($this->proxyServer) {
87: curl_setopt($curl, CURLOPT_PROXY, $this->proxyServer);
88: if ($this->proxyPort) {
89: curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxyPort);
90: }
91: if ($this->proxyUsername && $this->proxyPassword) {
92: curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyUsername . ':' . $this->proxyPassword);
93: curl_setopt($curl, CURLOPT_PROXYAUTH, $this->_httpAuthScheme($this->proxyAuthenticationScheme));
94: }
95: if ($this->proxyType == Horde_Http::PROXY_SOCKS5) {
96: curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
97: } else if ($this->proxyType != Horde_Http::PROXY_HTTP) {
98: throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
99: }
100: }
101:
102:
103: if ($this->username) {
104: curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
105: curl_setopt($curl, CURLOPT_HTTPAUTH, $this->_httpAuthScheme($this->authenticationScheme));
106: }
107:
108:
109: $hdr = array();
110: foreach ($this->headers as $header => $value) {
111: $hdr[] = $header . ': ' . $value;
112: }
113: curl_setopt($curl, CURLOPT_HTTPHEADER, $hdr);
114:
115: $result = curl_exec($curl);
116: if ($result === false) {
117: throw new Horde_Http_Exception(curl_error($curl), curl_errno($curl));
118: }
119: $info = curl_getinfo($curl);
120: return new Horde_Http_Response_Curl($this->uri, $result, $info);
121: }
122:
123: 124: 125: 126: 127: 128: 129:
130: protected function _httpAuthScheme($httpAuthScheme)
131: {
132: if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) {
133: throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')');
134: }
135: return $this->_httpAuthSchemes[$httpAuthScheme];
136: }
137: }
138: