From 0481920ed3f15047e54274a98449cd3909eadd01 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Thu, 22 May 2014 22:28:01 -0700 Subject: [PATCH] Fix #58: Include PHP_VERSION and curl version in user-agent --- src/Curl/Curl.php | 13 +++++++++++-- tests/PHPCurlClass/PHPCurlClassTest.php | 11 +++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 40e9383..a208b5a 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const USER_AGENT = 'PHP-Curl-Class/2.1.0 (+https://github.com/php-curl-class/php-curl-class)'; + const VERSION = '2.1.0'; private $cookies = array(); private $headers = array(); @@ -43,7 +43,7 @@ class Curl } $this->curl = curl_init(); - $this->setUserAgent(self::USER_AGENT); + $this->setDefaultUserAgent(); $this->setOpt(CURLINFO_HEADER_OUT, true); $this->setOpt(CURLOPT_HEADER, true); $this->setOpt(CURLOPT_RETURNTRANSFER, true); @@ -169,6 +169,15 @@ class Curl unset($this->headers[$key]); } + public function setDefaultUserAgent() + { + $user_agent = 'PHP-Curl-Class/' . self::VERSION . ' (+https://github.com/php-curl-class/php-curl-class)'; + $user_agent .= ' PHP/' . PHP_VERSION; + $curl_version = curl_version(); + $user_agent .= ' curl/' . $curl_version['version']; + $this->setUserAgent($user_agent); + } + public function setUserAgent($user_agent) { $this->setOpt(CURLOPT_USERAGENT, $user_agent); diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index bbed9b9..0394a43 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -73,11 +73,14 @@ class CurlTest extends PHPUnit_Framework_TestCase public function testUserAgent() { + $php_version = 'PHP\/' . PHP_VERSION; + $curl_version = curl_version(); + $curl_version = 'curl\/' . $curl_version['version']; + $test = new Test(); - $test->curl->setUserAgent(Curl::USER_AGENT); - $this->assertTrue($test->server('server', 'GET', array( - 'key' => 'HTTP_USER_AGENT', - )) === Curl::USER_AGENT); + $user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT')); + $this->assertRegExp('/' . $php_version . '/', $user_agent); + $this->assertRegExp('/' . $curl_version . '/', $user_agent); } public function testGet()