Advertisement
Guest User

Twilio curl client proxy workaround

a guest
Feb 15th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. namespace PaymentPlatform\PhoneBundle\lib\twilio\Http;
  4.  
  5. use Twilio\Exceptions\EnvironmentException;
  6. use Twilio\Http\Client;
  7. use Twilio\Http\Response;
  8.  
  9. class CurlProxyfiedClient implements Client {
  10.     const DEFAULT_TIMEOUT = 60;
  11.     protected $curlOptions = array();
  12.  
  13.     public function __construct(array $options = array()) {
  14.         $this->curlOptions = $options;
  15.     }
  16.  
  17.     public function request($method, $url, $params = array(), $data = array(),
  18.                             $headers = array(), $user = null, $password = null,
  19.                             $timeout = null) {
  20.         $options = $this->options($method, $url, $params, $data, $headers,
  21.                                   $user, $password, $timeout);
  22.  
  23.         try {
  24.             if (!$curl = curl_init()) {
  25.                 throw new EnvironmentException('Unable to initialize cURL');
  26.             }
  27.  
  28.             if (!curl_setopt_array($curl, $options)) {
  29.                 throw new EnvironmentException(curl_error($curl));
  30.             }
  31.  
  32.             if (!$response = curl_exec($curl)) {
  33.                 throw new EnvironmentException(curl_error($curl));
  34.             }
  35.  
  36.             $parts = explode("\r\n\r\n", $response, 3);
  37.  
  38.             if(array_key_exists(CURLOPT_PROXY, $options)) {
  39.                 list($head, $body) = array($parts[1], $parts[2]);
  40.             } else {
  41.                 list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
  42.                     ? array($parts[1], $parts[2])
  43.                     : array($parts[0], $parts[1]);
  44.             }
  45.  
  46.             $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  47.  
  48.             $responseHeaders = array();
  49.             $headerLines = explode("\r\n", $head);
  50.             array_shift($headerLines);
  51.             foreach ($headerLines as $line) {
  52.                 list($key, $value) = explode(':', $line, 2);
  53.                 $responseHeaders[$key] = $value;
  54.             }
  55.  
  56.             curl_close($curl);
  57.  
  58.             if (isset($buffer) && is_resource($buffer)) {
  59.                 fclose($buffer);
  60.             }
  61.  
  62.             return new Response($statusCode, $body, $responseHeaders);
  63.         } catch (\ErrorException $e) {
  64.             if (isset($curl) && is_resource($curl)) {
  65.                 curl_close($curl);
  66.             }
  67.  
  68.             if (isset($buffer) && is_resource($buffer)) {
  69.                 fclose($buffer);
  70.             }
  71.  
  72.             throw $e;
  73.         }
  74.     }
  75.  
  76.     public function options($method, $url, $params = array(), $data = array(),
  77.                             $headers = array(), $user = null, $password = null,
  78.                             $timeout = null) {
  79.  
  80.         $timeout = is_null($timeout)
  81.             ? self::DEFAULT_TIMEOUT
  82.             : $timeout;
  83.  
  84.         $options = $this->curlOptions + array(
  85.             CURLOPT_URL => $url,
  86.             CURLOPT_HEADER => true,
  87.             CURLOPT_RETURNTRANSFER => true,
  88.             CURLOPT_INFILESIZE => -1,
  89.             CURLOPT_HTTPHEADER => array(),
  90.             CURLOPT_TIMEOUT => $timeout,
  91.         );
  92.  
  93.         foreach ($headers as $key => $value) {
  94.             $options[CURLOPT_HTTPHEADER][] = "$key: $value";
  95.         }
  96.  
  97.         if ($user && $password) {
  98.             $options[CURLOPT_HTTPHEADER][] = 'Authorization: Basic ' . base64_encode("$user:$password");
  99.         }
  100.  
  101.         $body = $this->buildQuery($params);
  102.         if ($body) {
  103.             $options[CURLOPT_URL] .= '?' . $body;
  104.         }
  105.  
  106.         switch (strtolower(trim($method))) {
  107.             case 'get':
  108.                 $options[CURLOPT_HTTPGET] = true;
  109.                 break;
  110.             case 'post':
  111.                 $options[CURLOPT_POST] = true;
  112.                 $options[CURLOPT_POSTFIELDS] = $this->buildQuery($data);
  113.  
  114.                 break;
  115.             case 'put':
  116.                 $options[CURLOPT_PUT] = true;
  117.                 if ($data) {
  118.                     if ($buffer = fopen('php://memory', 'w+')) {
  119.                         $dataString = $this->buildQuery($data);
  120.                         fwrite($buffer, $dataString);
  121.                         fseek($buffer, 0);
  122.                         $options[CURLOPT_INFILE] = $buffer;
  123.                         $options[CURLOPT_INFILESIZE] = strlen($dataString);
  124.                     } else {
  125.                         throw new EnvironmentException('Unable to open a temporary file');
  126.                     }
  127.                 }
  128.                 break;
  129.             case 'head':
  130.                 $options[CURLOPT_NOBODY] = true;
  131.                 break;
  132.             default:
  133.                 $options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  134.         }
  135.  
  136.         return $options;
  137.     }
  138.  
  139.     public function buildQuery($params) {
  140.         $parts = array();
  141.  
  142.         $params = $params ?: array();
  143.  
  144.         foreach ($params as $key => $value) {
  145.             if (is_array($value)) {
  146.                 foreach ($value as $item) {
  147.                     $parts[] = urlencode((string)$key) . '=' . urlencode((string)$item);
  148.                 }
  149.             } else {
  150.                 $parts[] = urlencode((string)$key) . '=' . urlencode((string)$value);
  151.             }
  152.         }
  153.  
  154.         return implode('&', $parts);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement