summaryrefslogtreecommitdiffstats
path: root/Twilio/Http/CurlClient.php
diff options
context:
space:
mode:
Diffstat (limited to 'Twilio/Http/CurlClient.php')
-rw-r--r--Twilio/Http/CurlClient.php122
1 files changed, 68 insertions, 54 deletions
diff --git a/Twilio/Http/CurlClient.php b/Twilio/Http/CurlClient.php
index c397929..e296b2e 100644
--- a/Twilio/Http/CurlClient.php
+++ b/Twilio/Http/CurlClient.php
@@ -12,59 +12,8 @@ class CurlClient implements Client {
public function request($method, $url, $params = array(), $data = array(),
$headers = array(), $user = null, $password = null,
$timeout = null) {
- $timeout = is_null($timeout)
- ? self::DEFAULT_TIMEOUT
- : $timeout;
-
- $options = array(
- CURLOPT_URL => $url,
- CURLOPT_HEADER => true,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_INFILESIZE => -1,
- CURLOPT_HTTPHEADER => array(),
- CURLOPT_TIMEOUT => $timeout,
- );
-
- foreach ($headers as $key => $value) {
- $options[CURLOPT_HTTPHEADER][] = "$key: $value";
- }
-
- if ($user && $password) {
- $options[CURLOPT_HTTPHEADER][] = 'Authorization: Basic ' . base64_encode("$user:$password");
- }
-
- $body = $this->buildQuery($params);
-
- switch (strtolower(trim($method))) {
- case 'get':
- $options[CURLOPT_HTTPGET] = true;
- if ($body) {
- $options[CURLOPT_URL] = $options[CURLOPT_URL] . '?' . $body;
- }
- break;
- case 'post':
- $options[CURLOPT_POST] = true;
- $options[CURLOPT_POSTFIELDS] = $body;
- break;
- case 'put':
- $options[CURLOPT_PUT] = true;
- if ($body) {
- if ($buffer = fopen('php://memory', 'w+')) {
- fwrite($buffer, $body);
- fseek($buffer, 0);
- $options[CURLOPT_INFILE] = $buffer;
- $options[CURLOPT_INFILESIZE] = strlen($body);
- }
- } else {
- throw new EnvironmentException('Unable to open a temporary file');
- }
- break;
- case 'head':
- $opts[CURLOPT_NOBODY] = true;
- break;
- default:
- $opts[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
- }
+ $options = $this->options($method, $url, $params, $data, $headers,
+ $user, $password, $timeout);
try {
if (!$curl = curl_init()) {
@@ -114,9 +63,74 @@ class CurlClient implements Client {
}
}
- protected function buildQuery($params) {
+ public function options($method, $url, $params = array(), $data = array(),
+ $headers = array(), $user = null, $password = null,
+ $timeout = null) {
+
+ $timeout = is_null($timeout)
+ ? self::DEFAULT_TIMEOUT
+ : $timeout;
+
+ $options = array(
+ CURLOPT_URL => $url,
+ CURLOPT_HEADER => true,
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_INFILESIZE => -1,
+ CURLOPT_HTTPHEADER => array(),
+ CURLOPT_TIMEOUT => $timeout,
+ );
+
+ foreach ($headers as $key => $value) {
+ $options[CURLOPT_HTTPHEADER][] = "$key: $value";
+ }
+
+ if ($user && $password) {
+ $options[CURLOPT_HTTPHEADER][] = 'Authorization: Basic ' . base64_encode("$user:$password");
+ }
+
+ $body = $this->buildQuery($params);
+ if ($body) {
+ $options[CURLOPT_URL] .= '?' . $body;
+ }
+
+ switch (strtolower(trim($method))) {
+ case 'get':
+ $options[CURLOPT_HTTPGET] = true;
+ break;
+ case 'post':
+ $options[CURLOPT_POST] = true;
+ $options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
+
+ break;
+ case 'put':
+ $options[CURLOPT_PUT] = true;
+ if ($data) {
+ if ($buffer = fopen('php://memory', 'w+')) {
+ $dataString = $this->buildQuery($data);
+ fwrite($buffer, $dataString);
+ fseek($buffer, 0);
+ $options[CURLOPT_INFILE] = $buffer;
+ $options[CURLOPT_INFILESIZE] = strlen($dataString);
+ } else {
+ throw new EnvironmentException('Unable to open a temporary file');
+ }
+ }
+ break;
+ case 'head':
+ $options[CURLOPT_NOBODY] = true;
+ break;
+ default:
+ $options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
+ }
+
+ return $options;
+ }
+
+ public function buildQuery($params) {
$parts = array();
+ $params = $params ?: array();
+
foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $item) {