Skip to content

Commit 9b1b9f6

Browse files
aleeeftwseanmcn
authored andcommitted
This should allow multiple requests to be made with the curl library.
1 parent b1d4b54 commit 9b1b9f6

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea/
12
vendor/
23
coverage/

src/Kobas/Client.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ protected function call($http_method, $route, array $params = array(), array $he
140140
// $url .= trim($route, '/');
141141

142142
$this->request
143+
->init()
143144
->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($http_method))
144145
->setOption(CURLOPT_RETURNTRANSFER, true)
145146
->setOption(CURLOPT_FOLLOWLOCATION, true)
@@ -175,12 +176,13 @@ protected function call($http_method, $route, array $params = array(), array $he
175176

176177
$result = $this->request->execute();
177178
$last_response = $this->request->getInfo(CURLINFO_HTTP_CODE);
179+
180+
$this->request->close();
181+
178182
if ($last_response >= 400) {
179183
throw new HttpException($last_response, json_encode($result, true));
180184
}
181185

182-
$this->request->close();
183-
184186
return json_decode($result, true);
185187
}
186-
}
188+
}

src/Kobas/Request/Curl.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ class Curl implements HttpRequest
66
{
77
private $handle = null;
88

9-
public function __construct()
9+
public function init()
1010
{
11-
$this->handle = curl_init();
11+
if (is_null($this->handle)) {
12+
$this->handle = curl_init();
13+
}
14+
15+
return $this;
1216
}
1317

1418
public function setUrl($url)
@@ -35,6 +39,8 @@ public function getInfo($name)
3539
public function close()
3640
{
3741
curl_close($this->handle);
42+
$this->handle = null;
43+
3844
return $this;
3945
}
4046
}

src/Kobas/Request/HttpRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
interface HttpRequest
66
{
7+
/**
8+
* @return $this
9+
*/
10+
public function init();
11+
712
/**
813
* @param $url
914
* @return $this

0 commit comments

Comments
 (0)