Skip to content

Commit 0481920

Browse files
committed
Fix php-curl-class#58: Include PHP_VERSION and curl version in user-agent
1 parent 0fdbff4 commit 0481920

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Curl/Curl.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Curl
66
{
7-
const USER_AGENT = 'PHP-Curl-Class/2.1.0 (+https://github.com/php-curl-class/php-curl-class)';
7+
const VERSION = '2.1.0';
88

99
private $cookies = array();
1010
private $headers = array();
@@ -43,7 +43,7 @@ public function __construct()
4343
}
4444

4545
$this->curl = curl_init();
46-
$this->setUserAgent(self::USER_AGENT);
46+
$this->setDefaultUserAgent();
4747
$this->setOpt(CURLINFO_HEADER_OUT, true);
4848
$this->setOpt(CURLOPT_HEADER, true);
4949
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
@@ -169,6 +169,15 @@ public function unsetHeader($key)
169169
unset($this->headers[$key]);
170170
}
171171

172+
public function setDefaultUserAgent()
173+
{
174+
$user_agent = 'PHP-Curl-Class/' . self::VERSION . ' (+https://github.com/php-curl-class/php-curl-class)';
175+
$user_agent .= ' PHP/' . PHP_VERSION;
176+
$curl_version = curl_version();
177+
$user_agent .= ' curl/' . $curl_version['version'];
178+
$this->setUserAgent($user_agent);
179+
}
180+
172181
public function setUserAgent($user_agent)
173182
{
174183
$this->setOpt(CURLOPT_USERAGENT, $user_agent);

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ function assertions($array, $count = 1)
7373

7474
public function testUserAgent()
7575
{
76+
$php_version = 'PHP\/' . PHP_VERSION;
77+
$curl_version = curl_version();
78+
$curl_version = 'curl\/' . $curl_version['version'];
79+
7680
$test = new Test();
77-
$test->curl->setUserAgent(Curl::USER_AGENT);
78-
$this->assertTrue($test->server('server', 'GET', array(
79-
'key' => 'HTTP_USER_AGENT',
80-
)) === Curl::USER_AGENT);
81+
$user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
82+
$this->assertRegExp('/' . $php_version . '/', $user_agent);
83+
$this->assertRegExp('/' . $curl_version . '/', $user_agent);
8184
}
8285

8386
public function testGet()

0 commit comments

Comments
 (0)