Skip to content

Commit c41fbba

Browse files
authored
Merge pull request php-curl-class#388 from zachborboa/master
Add additional methods and fixes to MultiCurl
2 parents 2e08fc5 + 7ca7ac3 commit c41fbba

File tree

9 files changed

+162
-80
lines changed

9 files changed

+162
-80
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ php:
1212

1313
matrix:
1414
allow_failures:
15-
- php: 7.1
1615
- php: nightly
1716

1817
before_script:

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For latest commit version:
2929

3030
### Requirements
3131

32-
PHP Curl Class works with PHP 5.3, 5.4, 5.5, 5.6, 7.0, and HHVM.
32+
PHP Curl Class works with PHP 5.3, 5.4, 5.5, 5.6, 7.0, 7.1, and HHVM.
3333

3434
### Quick Start and Examples
3535

@@ -187,15 +187,13 @@ Curl::close()
187187
Curl::complete($callback)
188188
Curl::delete($url, $query_parameters = array(), $data = array())
189189
Curl::download($url, $mixed_filename)
190-
Curl::downloadComplete($fh)
191190
Curl::error($callback)
192191
Curl::exec($ch = null)
193192
Curl::get($url, $data = array())
194193
Curl::getCookie($key)
195194
Curl::getInfo($opt)
196195
Curl::getOpt($option)
197196
Curl::getResponseCookie($key)
198-
Curl::getResponseCookies()
199197
Curl::head($url, $data = array())
200198
Curl::headerCallback($ch, $header)
201199
Curl::options($url, $data = array())
@@ -250,13 +248,17 @@ MultiCurl::complete($callback)
250248
MultiCurl::error($callback)
251249
MultiCurl::getOpt($option)
252250
MultiCurl::setBasicAuthentication($username, $password = '')
251+
MultiCurl::setConnectTimeout($seconds)
253252
MultiCurl::setCookie($key, $value)
254253
MultiCurl::setCookieFile($cookie_file)
255254
MultiCurl::setCookieJar($cookie_jar)
255+
MultiCurl::setCookieString($string)
256256
MultiCurl::setDigestAuthentication($username, $password = '')
257257
MultiCurl::setHeader($key, $value)
258258
MultiCurl::setJsonDecoder($function)
259259
MultiCurl::setOpt($option, $value)
260+
MultiCurl::setOpts($options)
261+
MultiCurl::setPort($port)
260262
MultiCurl::setReferer($referer)
261263
MultiCurl::setReferrer($referrer)
262264
MultiCurl::setTimeout($seconds)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"ext-curl": "*"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "*"
20+
"phpunit/phpunit": "*",
21+
"squizlabs/php_codesniffer": "*"
2122
},
2223
"autoload": {
2324
"psr-4": {

src/Curl/Curl.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Curl
6060
public $requestHeaders = null;
6161
public $responseHeaders = null;
6262
public $rawResponseHeaders = '';
63+
public $responseCookies = array();
6364
public $response = null;
6465
public $rawResponse = null;
6566

@@ -71,7 +72,6 @@ class Curl
7172
public $fileHandle = null;
7273

7374
private $cookies = array();
74-
private $responseCookies = array();
7575
private $headers = array();
7676
private $options = array();
7777

@@ -257,10 +257,10 @@ public function delete($url, $query_parameters = array(), $data = array())
257257
/**
258258
* Download Complete
259259
*
260-
* @access public
260+
* @access private
261261
* @param $fh
262262
*/
263-
public function downloadComplete($fh)
263+
private function downloadComplete($fh)
264264
{
265265
if (!$this->error && $this->downloadCompleteFunction) {
266266
rewind($fh);
@@ -336,8 +336,8 @@ public function error($callback)
336336
*/
337337
public function exec($ch = null)
338338
{
339-
$this->responseCookies = array();
340339
if ($ch === null) {
340+
$this->responseCookies = array();
341341
$this->call($this->beforeSendFunction);
342342
$this->rawResponse = curl_exec($this->curl);
343343
$this->curlErrorCode = curl_errno($this->curl);
@@ -437,7 +437,7 @@ public function getInfo($opt)
437437
*/
438438
public function getOpt($option)
439439
{
440-
return $this->options[$option];
440+
return isset($this->options[$option]) ? $this->options[$option] : null;
441441
}
442442

443443
/**
@@ -727,18 +727,6 @@ public function getResponseCookie($key)
727727
return isset($this->responseCookies[$key]) ? $this->responseCookies[$key] : null;
728728
}
729729

730-
/**
731-
* Get response cookies.
732-
*
733-
* @access public
734-
*
735-
* @return array
736-
*/
737-
public function getResponseCookies()
738-
{
739-
return $this->responseCookies;
740-
}
741-
742730
/**
743731
* Set Port
744732
*

src/Curl/MultiCurl.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ public function beforeSend($callback)
303303
*/
304304
public function close()
305305
{
306-
foreach ($this->curls as $ch) {
307-
$ch->close();
306+
foreach ($this->curls as $curl) {
307+
$curl->close();
308308
}
309309

310310
if (is_resource($this->multiCurl)) {
@@ -344,7 +344,7 @@ public function error($callback)
344344
*/
345345
public function getOpt($option)
346346
{
347-
return $this->options[$option];
347+
return isset($this->options[$option]) ? $this->options[$option] : null;
348348
}
349349

350350
/**
@@ -383,7 +383,41 @@ public function setDigestAuthentication($username, $password = '')
383383
public function setCookie($key, $value)
384384
{
385385
$this->cookies[$key] = $value;
386-
$this->setOpt(CURLOPT_COOKIE, str_replace('+', '%20', http_build_query($this->cookies, '', '; ')));
386+
}
387+
388+
/**
389+
* Set Port
390+
*
391+
* @access public
392+
* @param $port
393+
*/
394+
public function setPort($port)
395+
{
396+
$this->setOpt(CURLOPT_PORT, intval($port));
397+
}
398+
399+
/**
400+
* Set Connect Timeout
401+
*
402+
* @access public
403+
* @param $seconds
404+
*/
405+
public function setConnectTimeout($seconds)
406+
{
407+
$this->setOpt(CURLOPT_CONNECTTIMEOUT, $seconds);
408+
}
409+
410+
/**
411+
* Set Cookie String
412+
*
413+
* @access public
414+
* @param $string
415+
*
416+
* @return bool
417+
*/
418+
public function setCookieString($string)
419+
{
420+
return $this->setOpt(CURLOPT_COOKIE, $string);
387421
}
388422

389423
/**
@@ -458,6 +492,19 @@ public function setOpt($option, $value)
458492
$this->options[$option] = $value;
459493
}
460494

495+
/**
496+
* Set Opts
497+
*
498+
* @access public
499+
* @param $options
500+
*/
501+
public function setOpts($options)
502+
{
503+
foreach ($options as $option => $value) {
504+
$this->setOpt($option, $value);
505+
}
506+
}
507+
461508
/**
462509
* Set Referer
463510
*
@@ -662,6 +709,9 @@ private function initHandle($curl)
662709
foreach ($this->headers as $key => $value) {
663710
$curl->setHeader($key, $value);
664711
}
712+
foreach ($this->cookies as $key => $value) {
713+
$curl->setCookie($key, $value);
714+
}
665715
$curl->setJsonDecoder($this->jsonDecoder);
666716
$curl->setXmlDecoder($this->xmlDecoder);
667717

@@ -671,6 +721,7 @@ private function initHandle($curl)
671721
}
672722

673723
$this->activeCurls[$curl->id] = $curl;
724+
$this->responseCookies = array();
674725
$curl->call($curl->beforeSendFunction);
675726
}
676727
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,53 +201,46 @@ public function testSetUrl()
201201
$data = array('key' => 'value');
202202

203203
$curl = new Curl();
204-
$curl->setHeader('X-DEBUG-TEST', 'get');
205204
$curl->setUrl(Test::TEST_URL);
206205
$curl->delete($data);
207-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
208-
$this->assertEquals('key=value', $curl->response);
206+
$this->assertEquals('DELETE /?key=value HTTP/1.1', $curl->requestHeaders['Request-Line']);
207+
$this->assertEquals(Test::TEST_URL . '?key=value', $curl->effectiveUrl);
209208

210209
$curl = new Curl();
211-
$curl->setHeader('X-DEBUG-TEST', 'get');
212210
$curl->setUrl(Test::TEST_URL);
213211
$curl->get($data);
214-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
215-
$this->assertEquals('key=value', $curl->response);
212+
$this->assertEquals('GET /?key=value HTTP/1.1', $curl->requestHeaders['Request-Line']);
213+
$this->assertEquals(Test::TEST_URL . '?key=value', $curl->effectiveUrl);
216214

217215
$curl = new Curl();
218-
$curl->setHeader('X-DEBUG-TEST', 'get');
219216
$curl->setUrl(Test::TEST_URL);
220217
$curl->head($data);
221-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
222218
$this->assertEquals('HEAD /?key=value HTTP/1.1', $curl->requestHeaders['Request-Line']);
219+
$this->assertEquals(Test::TEST_URL . '?key=value', $curl->effectiveUrl);
223220

224221
$curl = new Curl();
225-
$curl->setHeader('X-DEBUG-TEST', 'get');
226222
$curl->setUrl(Test::TEST_URL);
227223
$curl->options($data);
228-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
229-
$this->assertEquals('key=value', $curl->response);
224+
$this->assertEquals('OPTIONS /?key=value HTTP/1.1', $curl->requestHeaders['Request-Line']);
225+
$this->assertEquals(Test::TEST_URL . '?key=value', $curl->effectiveUrl);
230226

231227
$curl = new Curl();
232-
$curl->setHeader('X-DEBUG-TEST', 'request_method');
233228
$curl->setUrl(Test::TEST_URL);
234229
$curl->patch($data);
235-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
236-
$this->assertEquals('PATCH', $curl->response);
230+
$this->assertEquals('PATCH / HTTP/1.1', $curl->requestHeaders['Request-Line']);
231+
$this->assertEquals(Test::TEST_URL, $curl->effectiveUrl);
237232

238233
$curl = new Curl();
239-
$curl->setHeader('X-DEBUG-TEST', 'post');
240234
$curl->setUrl(Test::TEST_URL);
241235
$curl->post($data);
242-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
243-
$this->assertEquals('key=value', $curl->response);
236+
$this->assertEquals('POST / HTTP/1.1', $curl->requestHeaders['Request-Line']);
237+
$this->assertEquals(Test::TEST_URL, $curl->effectiveUrl);
244238

245239
$curl = new Curl();
246-
$curl->setHeader('X-DEBUG-TEST', 'put');
247240
$curl->setUrl(Test::TEST_URL);
248241
$curl->put($data);
249-
$this->assertEquals(Test::TEST_URL, $curl->baseUrl);
250-
$this->assertEquals('key=value', $curl->response);
242+
$this->assertEquals('PUT / HTTP/1.1', $curl->requestHeaders['Request-Line']);
243+
$this->assertEquals(Test::TEST_URL, $curl->effectiveUrl);
251244
}
252245

253246
public function testEffectiveUrl()
@@ -877,9 +870,8 @@ public function testMultipleCookieResponse()
877870
$test->server('multiple_cookie', 'GET');
878871
$this->assertEquals('cookie1=scrumptious,cookie2=mouthwatering', $test->curl->responseHeaders['Set-Cookie']);
879872

880-
$response_cookies = $test->curl->getResponseCookies();
881-
$this->assertEquals('scrumptious', $response_cookies['cookie1']);
882-
$this->assertEquals('mouthwatering', $response_cookies['cookie2']);
873+
$this->assertEquals('scrumptious', $test->curl->responseCookies['cookie1']);
874+
$this->assertEquals('mouthwatering', $test->curl->responseCookies['cookie2']);
883875
}
884876

885877
public function testDefaultTimeout()

0 commit comments

Comments
 (0)