Skip to content

Commit de1368e

Browse files
committed
Adding ability to override the built-in cURL parameters.
Previously, these were hard-coded, but now it is possible to add additional parameters, or to change the hard-coded values. Some use-cases that I have already encountered (there are no doubt many more): * The ability to set CURLOPT_STDERR (and to set CURLOPT_VERBOSE to true), to aid debugging. * The ability to set CURLOPT_SSL_VERIFYPEER to false if you don't have appropriate root CA certificates set up, or to set CURLOPT_CAINFO to an appropriate path if cURL isn't picking this up automatically. Default behaviour is unchanged.
1 parent 9cb74ee commit de1368e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/Google/Spreadsheet/DefaultServiceRequest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class DefaultServiceRequest implements ServiceRequestInterface
6060
*/
6161
protected $userAgent = 'PHP Google Spreadsheet Api';
6262

63+
/**
64+
* Additional cURL parameters, or overrides for default parameters.
65+
*
66+
* @var array
67+
*/
68+
protected $curlParams = array();
69+
6370
/**
6471
* Initializes the service request object.
6572
*
@@ -118,6 +125,33 @@ public function setUserAgent($userAgent)
118125
return $this;
119126
}
120127

128+
/**
129+
* Get explicitly-set cURL parameters.
130+
* Does not include the default parameters - just the parameters explicitly set
131+
* by the user.
132+
*
133+
* @return array
134+
*/
135+
public function getCurlParams()
136+
{
137+
return $this->curlParams;
138+
}
139+
140+
/**
141+
* Set optional cURL parameters, which can add to or override the default
142+
* parameters used for our web requests.
143+
* WARNING: Certain over-rides may prevent the code from working. Use with care!
144+
*
145+
* @param array $curlParams associative array of key value pairs
146+
*
147+
* @return Google\Spreadsheet\DefaultServiceRequest
148+
*/
149+
public function setCurlParams(array $curlParams)
150+
{
151+
$this->curlParams = $curlParams;
152+
return $this;
153+
}
154+
121155
/**
122156
* Perform a get request
123157
*
@@ -204,6 +238,10 @@ protected function initRequest($url, $requestHeaders = array())
204238
CURLOPT_VERBOSE => false,
205239
);
206240

241+
// Need to use array addition, rather than array_merge(), as the array is
242+
// indexed by numeric keys which we don't want to end up re-indexing.
243+
$curlParams = $this->getCurlParams() + $curlParams;
244+
207245
if(substr($url, 0, 4) !== 'http') {
208246
$url = $this->serviceUrl . $url;
209247
}

0 commit comments

Comments
 (0)