From ed37de7d87d77bd3ef9d24bae874a19973c97ee0 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Fri, 4 Nov 2016 02:24:50 -0700 Subject: [PATCH 1/2] Fix #404: Allow calling Curl::getInfo() without option --- README.md | 2 +- src/Curl/Curl.php | 11 +++++-- tests/PHPCurlClass/PHPCurlClassTest.php | 41 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a86017f..461ecb3 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ Curl::error($callback) Curl::exec($ch = null) Curl::get($url, $data = array()) Curl::getCookie($key) -Curl::getInfo($opt) +Curl::getInfo($opt = null) Curl::getOpt($option) Curl::getResponseCookie($key) Curl::head($url, $data = array()) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index a8e31a5..47c4914 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -422,9 +422,16 @@ class Curl * * @return mixed */ - public function getInfo($opt) + public function getInfo($opt = null) { - return curl_getinfo($this->curl, $opt); + $args = array(); + $args[] = $this->curl; + + if (func_num_args()) { + $args[] = $opt; + } + + return call_user_func_array('curl_getinfo', $args); } /** diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index ec7d81e..54c3193 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -3000,4 +3000,45 @@ class CurlTest extends PHPUnit_Framework_TestCase $curl->get(Test::TEST_URL, $data); $this->assertEquals('', $curl->response); } + + public function testGetInfo() + { + $test = new Test(); + $test->server('server', 'GET'); + $info = $test->curl->getInfo(); + + $expected_keys = array( + 'url', + 'content_type', + 'http_code', + 'header_size', + 'request_size', + 'filetime', + 'ssl_verify_result', + 'redirect_count', + 'total_time', + 'namelookup_time', + 'connect_time', + 'pretransfer_time', + 'size_upload', + 'size_download', + 'speed_download', + 'speed_upload', + 'download_content_length', + 'upload_content_length', + 'starttransfer_time', + 'redirect_time', + 'certinfo', + 'primary_ip', + 'primary_port', + 'local_ip', + 'local_port', + 'redirect_url', + 'request_header', + ); + + foreach ($expected_keys as $key) { + $this->assertArrayHasKey($key, $info); + } + } } From 9bfdd791cdbe903f97ac553986b2a4e009743742 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Fri, 4 Nov 2016 21:10:57 -0700 Subject: [PATCH 2/2] Remove keys not found in curl_getinfo() response on select systems --- tests/PHPCurlClass/PHPCurlClassTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 54c3193..0ee82a6 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -3037,6 +3037,22 @@ class CurlTest extends PHPUnit_Framework_TestCase 'request_header', ); + // Not all keys are included on PHP 5.3 (tested 5.3.29). + if (version_compare(PHP_VERSION, '5.4.0', '<')) { + foreach (array('primary_ip', 'primary_port', 'local_ip', 'local_port') as $value) { + $key = array_search($value, $expected_keys); + unset($expected_keys[$key]); + } + } + + // Not all keys are included on HHVM (tested 3.6.6). + if (defined('HHVM_VERSION')) { + foreach (array('certinfo', 'primary_ip', 'primary_port', 'local_ip', 'redirect_url') as $value) { + $key = array_search($value, $expected_keys); + unset($expected_keys[$key]); + } + } + foreach ($expected_keys as $key) { $this->assertArrayHasKey($key, $info); }