Skip to content

Commit 86b4ef3

Browse files
committed
Merge branch 'MarkMaldaba-allow_curl_param_overrides'
2 parents 080a91d + c8a9aa0 commit 86b4ef3

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/Google/Spreadsheet/DefaultServiceRequest.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DefaultServiceRequest implements ServiceRequestInterface
4747
*
4848
* @var array
4949
*/
50-
protected $headers = array();
50+
protected $headers = [];
5151

5252
/**
5353
* Service url
@@ -70,6 +70,19 @@ class DefaultServiceRequest implements ServiceRequestInterface
7070
*/
7171
protected $sslVerifyPeer = true;
7272

73+
/**
74+
* cURL parameters
75+
*
76+
* @var array
77+
*/
78+
protected $curlParams = [
79+
CURLOPT_RETURNTRANSFER => true,
80+
CURLOPT_FOLLOWLOCATION => true,
81+
CURLOPT_FAILONERROR => false,
82+
CURLOPT_SSL_VERIFYPEER => true,
83+
CURLOPT_VERBOSE => false,
84+
];
85+
7386
/**
7487
* Initializes the service request object.
7588
*
@@ -174,7 +187,7 @@ public function setUserAgent($userAgent)
174187
*/
175188
public function getSslVerifyPeer()
176189
{
177-
return $this->sslVerifyPeer;
190+
return $this->curlParams[CURLOPT_SSL_VERIFYPEER];
178191
}
179192

180193
/**
@@ -186,7 +199,31 @@ public function getSslVerifyPeer()
186199
*/
187200
public function setSslVerifyPeer($sslVerifyPeer)
188201
{
189-
$this->sslVerifyPeer = (bool) $sslVerifyPeer;
202+
$this->curlParams[CURLOPT_SSL_VERIFYPEER] = (bool) $sslVerifyPeer;
203+
return $this;
204+
}
205+
206+
/**
207+
* Get currently set curl params
208+
*
209+
* @return array
210+
*/
211+
public function getCurlParams()
212+
{
213+
return $this->curlParams;
214+
}
215+
216+
/**
217+
* Add an extra curl parameter or override an existing one
218+
*
219+
* @param string $name 'CURLOPT_*' constant
220+
* @param mixed $value
221+
*
222+
* @return DefaultServiceRequest
223+
*/
224+
public function addCurlParam($name, $value)
225+
{
226+
$this->curlParams[$name] = $value;
190227
return $this;
191228
}
192229

@@ -268,20 +305,12 @@ public function delete($url)
268305
*/
269306
protected function initRequest($url, $requestHeaders = array())
270307
{
271-
$curlParams = array (
272-
CURLOPT_RETURNTRANSFER => true,
273-
CURLOPT_FOLLOWLOCATION => true,
274-
CURLOPT_FAILONERROR => false,
275-
CURLOPT_SSL_VERIFYPEER => $this->sslVerifyPeer,
276-
CURLOPT_VERBOSE => false,
277-
);
278-
279308
if(substr($url, 0, 4) !== "http") {
280309
$url = $this->serviceUrl . $url;
281310
}
282311

283312
$ch = curl_init();
284-
curl_setopt_array($ch, $curlParams);
313+
curl_setopt_array($ch, $this->curlParams);
285314
curl_setopt($ch, CURLOPT_URL, $url);
286315

287316
$headers = array();

tests/Google/Spreadsheet/DefaultServiceRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public function testSetSslVerifyPeer()
6464
$this->assertFalse($request->getSslVerifyPeer());
6565
}
6666

67+
public function testGetCurlParams()
68+
{
69+
$request = new DefaultServiceRequest("token");
70+
$this->assertTrue(count($request->getCurlParams()) === 5);
71+
}
72+
73+
public function testAddCurlParam()
74+
{
75+
$request = new DefaultServiceRequest("token");
76+
$request->addCurlParam(CURLOPT_SSL_VERIFYPEER, false);
77+
78+
$params = $request->getCurlParams();
79+
80+
$this->assertFalse($params[CURLOPT_SSL_VERIFYPEER]);
81+
}
82+
6783
public function testGet()
6884
{
6985
$mockRequest = $this->getMockBuilder(DefaultServiceRequest::class)

0 commit comments

Comments
 (0)