public function request($method, $absUrl, $headers, $params, $hasFile) { $curl = curl_init(); $method = strtolower($method); $opts = array(); if ($method == 'get') { if ($hasFile) { throw new Error\Api("Issuing a GET request with a file parameter"); } $opts[CURLOPT_HTTPGET] = 1; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } elseif ($method == 'post') { $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : self::encode($params); } elseif ($method == 'delete') { $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } else { throw new Error\Api("Unrecognized method {$method}"); } $absUrl = Util\Util::utf8($absUrl); $opts[CURLOPT_URL] = $absUrl; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_CONNECTTIMEOUT] = 30; $opts[CURLOPT_TIMEOUT] = 80; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_HTTPHEADER] = $headers; if (!Stripe::$verifySslCerts) { $opts[CURLOPT_SSL_VERIFYPEER] = false; } curl_setopt_array($curl, $opts); $rbody = curl_exec($curl); if (!defined('CURLE_SSL_CACERT_BADFILE')) { define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP } $errno = curl_errno($curl); if ($errno == CURLE_SSL_CACERT || $errno == CURLE_SSL_PEER_CERTIFICATE || $errno == CURLE_SSL_CACERT_BADFILE) { array_push($headers, 'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'); $cert = self::caBundle(); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CAINFO, $cert); $rbody = curl_exec($curl); } if ($rbody === false) { $errno = curl_errno($curl); $message = curl_error($curl); curl_close($curl); $this->handleCurlError($absUrl, $errno, $message); } $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return array($rbody, $rcode); }
/** * @param mixed $d * * @return array|mixed|string */ private static function _encodeObjects($d) { if ($d instanceof ApiResource) { return Util\Util::utf8($d->id); } elseif ($d === true) { return 'true'; } elseif ($d === false) { return 'false'; } elseif (is_array($d)) { $res = array(); foreach ($d as $k => $v) { $res[$k] = self::_encodeObjects($v); } return $res; } else { return Util\Util::utf8($d); } }
public function __toArray($recursive = false) { if ($recursive) { return Util\Util::convertStripeObjectToArray($this->_values); } else { return $this->_values; } }
public function request($method, $absUrl, $headers, $params, $hasFile) { $curl = curl_init(); $method = strtolower($method); $opts = array(); if (is_callable($this->defaultOptions)) { // call defaultOptions callback, set options to return value $opts = call_user_func_array($this->defaultOptions, func_get_args()); if (!is_array($opts)) { throw new Error\Api("Non-array value returned by defaultOptions CurlClient callback"); } } elseif (is_array($this->defaultOptions)) { // set default curlopts from array $opts = $this->defaultOptions; } if ($method == 'get') { if ($hasFile) { throw new Error\Api("Issuing a GET request with a file parameter"); } $opts[CURLOPT_HTTPGET] = 1; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } elseif ($method == 'post') { $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : self::encode($params); } elseif ($method == 'delete') { $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } else { throw new Error\Api("Unrecognized method {$method}"); } // Create a callback to capture HTTP headers for the response $rheaders = array(); $headerCallback = function ($curl, $header_line) use(&$rheaders) { // Ignore the HTTP request line (HTTP/1.1 200 OK) if (strpos($header_line, ":") === false) { return strlen($header_line); } list($key, $value) = explode(":", trim($header_line), 2); $rheaders[trim($key)] = trim($value); return strlen($header_line); }; // By default for large request body sizes (> 1024 bytes), cURL will // send a request without a body and with a `Expect: 100-continue` // header, which gives the server a chance to respond with an error // status code in cases where one can be determined right away (say // on an authentication problem for example), and saves the "large" // request body from being ever sent. // // Unfortunately, the bindings don't currently correctly handle the // success case (in which the server sends back a 100 CONTINUE), so // we'll error under that condition. To compensate for that problem // for the time being, override cURL's behavior by simply always // sending an empty `Expect:` header. array_push($headers, 'Expect: '); $absUrl = Util\Util::utf8($absUrl); $opts[CURLOPT_URL] = $absUrl; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout; $opts[CURLOPT_TIMEOUT] = $this->timeout; $opts[CURLOPT_HEADERFUNCTION] = $headerCallback; $opts[CURLOPT_HTTPHEADER] = $headers; if (!Stripe::$verifySslCerts) { $opts[CURLOPT_SSL_VERIFYPEER] = false; } // @codingStandardsIgnoreStart // PSR2 requires all constants be upper case. Sadly, the CURL_SSLVERSION // constants to not abide by those rules. // // Explicitly set a TLS version for cURL to use now that we're starting // to block 1.0 and 1.1 requests. // // If users are on OpenSSL >= 1.0.1, we know that they support TLS 1.2, // so set that explicitly because on some older Linux distros, clients may // default to TLS 1.0 even when they have TLS 1.2 available. // // For users on much older versions of OpenSSL, set a valid range of // TLS 1.0 to 1.2 (CURL_SSLVERSION_TLSv1). Note that this may result in // their requests being blocked unless they're specially flagged into // being able to use an old TLS version. // // Note: The int on the right is pulled from the source of OpenSSL 1.0.1a. if (OPENSSL_VERSION_NUMBER >= 0x1000100f) { if (!defined('CURL_SSLVERSION_TLSv1_2')) { // Note the value 6 comes from its position in the enum that // defines it in cURL's source code. define('CURL_SSLVERSION_TLSv1_2', 6); // constant not defined in PHP < 5.5 } $opts[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2; } else { if (!defined('CURL_SSLVERSION_TLSv1')) { define('CURL_SSLVERSION_TLSv1', 1); // constant not defined in PHP < 5.5 } $opts[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1; } // @codingStandardsIgnoreEnd curl_setopt_array($curl, $opts); $rbody = curl_exec($curl); if (!defined('CURLE_SSL_CACERT_BADFILE')) { define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP } $errno = curl_errno($curl); if ($errno == CURLE_SSL_CACERT || $errno == CURLE_SSL_PEER_CERTIFICATE || $errno == CURLE_SSL_CACERT_BADFILE) { array_push($headers, 'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'); $cert = self::caBundle(); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CAINFO, $cert); $rbody = curl_exec($curl); } if ($rbody === false) { $errno = curl_errno($curl); $message = curl_error($curl); curl_close($curl); $this->handleCurlError($absUrl, $errno, $message); } $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return array($rbody, $rcode, $rheaders); }
public function request($method, $absUrl, $headers, $params, $hasFile) { $curl = curl_init(); $method = strtolower($method); $opts = array(); if ($method == 'get') { if ($hasFile) { throw new Error\Api("Issuing a GET request with a file parameter"); } $opts[CURLOPT_HTTPGET] = 1; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } elseif ($method == 'post') { $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : self::encode($params); } elseif ($method == 'delete') { $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } else { throw new Error\Api("Unrecognized method {$method}"); } // Create a callback to capture HTTP headers for the response $rheaders = array(); $headerCallback = function ($curl, $header_line) use(&$rheaders) { // Ignore the HTTP request line (HTTP/1.1 200 OK) if (strpos($header_line, ":") === false) { return strlen($header_line); } list($key, $value) = explode(":", trim($header_line), 2); $rheaders[trim($key)] = trim($value); return strlen($header_line); }; $absUrl = Util\Util::utf8($absUrl); $opts[CURLOPT_URL] = $absUrl; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout; $opts[CURLOPT_TIMEOUT] = $this->timeout; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_HEADERFUNCTION] = $headerCallback; $opts[CURLOPT_HTTPHEADER] = $headers; if (!Stripe::$verifySslCerts) { $opts[CURLOPT_SSL_VERIFYPEER] = false; } // @codingStandardsIgnoreStart // PSR2 requires all constants be upper case. Sadly, the CURL_SSLVERSION // constants to not abide by those rules. // // Opt into TLS 1.x support on older versions of curl. This causes some // curl versions, notably on RedHat, to upgrade the connection to TLS // 1.2, from the default TLS 1.0. if (!defined('CURL_SSLVERSION_TLSv1')) { define('CURL_SSLVERSION_TLSv1', 1); // constant not defined in PHP < 5.5 } $opts[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1; // @codingStandardsIgnoreEnd curl_setopt_array($curl, $opts); $rbody = curl_exec($curl); if (!defined('CURLE_SSL_CACERT_BADFILE')) { define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP } $errno = curl_errno($curl); if ($errno == CURLE_SSL_CACERT || $errno == CURLE_SSL_PEER_CERTIFICATE || $errno == CURLE_SSL_CACERT_BADFILE) { array_push($headers, 'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'); $cert = self::caBundle(); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CAINFO, $cert); $rbody = curl_exec($curl); } if ($rbody === false) { $errno = curl_errno($curl); $message = curl_error($curl); curl_close($curl); $this->handleCurlError($absUrl, $errno, $message); } $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return array($rbody, $rcode, $rheaders); }
public function request($method, $absUrl, $headers, $params, $hasFile) { $curl = curl_init(); $method = strtolower($method); $opts = array(); if (is_callable($this->defaultOptions)) { // call defaultOptions callback, set options to return value $opts = call_user_func_array($this->defaultOptions, func_get_args()); if (!is_array($opts)) { throw new Error\Api("Non-array value returned by defaultOptions CurlClient callback"); } } elseif (is_array($this->defaultOptions)) { // set default curlopts from array $opts = $this->defaultOptions; } if ($method == 'get') { if ($hasFile) { throw new Error\Api("Issuing a GET request with a file parameter"); } $opts[CURLOPT_HTTPGET] = 1; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } elseif ($method == 'post') { $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : self::encode($params); } elseif ($method == 'delete') { $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; if (count($params) > 0) { $encoded = self::encode($params); $absUrl = "{$absUrl}?{$encoded}"; } } else { throw new Error\Api("Unrecognized method {$method}"); } // Create a callback to capture HTTP headers for the response $rheaders = array(); $headerCallback = function ($curl, $header_line) use(&$rheaders) { // Ignore the HTTP request line (HTTP/1.1 200 OK) if (strpos($header_line, ":") === false) { return strlen($header_line); } list($key, $value) = explode(":", trim($header_line), 2); $rheaders[trim($key)] = trim($value); return strlen($header_line); }; // By default for large request body sizes (> 1024 bytes), cURL will // send a request without a body and with a `Expect: 100-continue` // header, which gives the server a chance to respond with an error // status code in cases where one can be determined right away (say // on an authentication problem for example), and saves the "large" // request body from being ever sent. // // Unfortunately, the bindings don't currently correctly handle the // success case (in which the server sends back a 100 CONTINUE), so // we'll error under that condition. To compensate for that problem // for the time being, override cURL's behavior by simply always // sending an empty `Expect:` header. array_push($headers, 'Expect: '); $absUrl = Util\Util::utf8($absUrl); $opts[CURLOPT_URL] = $absUrl; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout; $opts[CURLOPT_TIMEOUT] = $this->timeout; $opts[CURLOPT_HEADERFUNCTION] = $headerCallback; $opts[CURLOPT_HTTPHEADER] = $headers; if (!Stripe::$verifySslCerts) { $opts[CURLOPT_SSL_VERIFYPEER] = false; } curl_setopt_array($curl, $opts); $rbody = curl_exec($curl); if (!defined('CURLE_SSL_CACERT_BADFILE')) { define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP } $errno = curl_errno($curl); if ($errno == CURLE_SSL_CACERT || $errno == CURLE_SSL_PEER_CERTIFICATE || $errno == CURLE_SSL_CACERT_BADFILE) { array_push($headers, 'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'); $cert = self::caBundle(); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CAINFO, $cert); $rbody = curl_exec($curl); } if ($rbody === false) { $errno = curl_errno($curl); $message = curl_error($curl); curl_close($curl); $this->handleCurlError($absUrl, $errno, $message); } $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return array($rbody, $rcode, $rheaders); }