Skip to content

Commit bd7fd93

Browse files
committed
guess connection port number + throw Exception on connection error
1 parent 099236a commit bd7fd93

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

lib/Redmine/Client.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
*/
1010
class Client
1111
{
12+
/**
13+
* @var array
14+
*/
15+
private static $defaultPorts = array(
16+
'http' => 80,
17+
'https' => 443,
18+
);
19+
1220
/**
1321
* @var string
1422
*/
@@ -163,21 +171,32 @@ public function delete($path)
163171
* @param string $method
164172
* @param string $data
165173
* @return false|SimpleXMLElement|string
174+
* @throws \Exception If anything goes wrong on curl request
166175
*/
167176
private function runRequest($path, $method = 'GET', $data = '')
168177
{
178+
$tmp = parse_url($this->url.$path);
179+
180+
if (isset($tmp['port'])) {
181+
$port = $tmp['port'];
182+
} else {
183+
$port = self::$defaultPorts[$tmp['scheme']];
184+
}
185+
169186
$curl = curl_init();
170187
if (isset($this->apikey)) {
171188
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
172189
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
173190
}
174191
curl_setopt($curl, CURLOPT_URL, $this->url.$path);
175-
curl_setopt($curl, CURLOPT_PORT , 80);
176192
curl_setopt($curl, CURLOPT_VERBOSE, 0);
177193
curl_setopt($curl, CURLOPT_HEADER, 0);
178194
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
195+
curl_setopt($curl, CURLOPT_PORT , $port);
196+
if (80 !== $port) {
197+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
198+
}
179199

180-
$tmp = parse_url($path);
181200
if ('xml' === substr($tmp['path'], -3)) {
182201
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
183202
'Content-Type: text/xml',
@@ -196,20 +215,18 @@ private function runRequest($path, $method = 'GET', $data = '')
196215
case 'DELETE':
197216
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
198217
break;
199-
default: // get
218+
default: // GET
200219
break;
201220
}
202-
try {
203-
$response = curl_exec($curl);
204-
if (curl_errno($curl)) {
205-
curl_close($curl);
221+
$response = curl_exec($curl);
206222

207-
return false;
208-
}
223+
if (curl_errno($curl)) {
224+
$e = new \Exception(curl_error($curl), curl_errno($curl));
209225
curl_close($curl);
210-
} catch (\Exception $e) {
211-
return false;
226+
throw $e;
212227
}
228+
curl_close($curl);
229+
213230
if ($response) {
214231
if ('<' === substr($response, 0, 1)) {
215232
return new \SimpleXMLElement($response);

0 commit comments

Comments
 (0)