summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl
diff options
context:
space:
mode:
Diffstat (limited to 'src/PurpleCode/PCurl')
-rw-r--r--src/PurpleCode/PCurl/PCurl.php193
-rw-r--r--src/PurpleCode/PCurl/PCurlException.php40
2 files changed, 233 insertions, 0 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php
new file mode 100644
index 0000000..7102661
--- /dev/null
+++ b/src/PurpleCode/PCurl/PCurl.php
@@ -0,0 +1,193 @@
+<?php
+/**
+ * PCurl is a REST client libary for PHP.
+ *
+ * See http://github.com/purplecode/php.curl for details.
+ *
+ * This code is licensed for use, modification, and distribution
+ * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
+ */
+
+namespace PurpleCode\PCurl;
+
+require_once 'PCurlException.php';
+
+use PurpleCode\PCurl\PCurlException;
+
+class PCurl {
+
+ private $options;
+ private $host;
+ private $headers;
+
+ public function __construct($host) {
+
+ if (!function_exists('curl_init')) {
+ throw new PCurlException('CURL module not available! See http://php.net/manual/en/book.curl.php');
+ }
+
+ $this->host = $host;
+ $this->headers = array();
+ $this->options = array();
+
+ $this->setOption(CURLOPT_TIMEOUT, 30);
+ // forced to use SSL3.0
+ $this->setOption(CURLOPT_SSLVERSION, 3);
+ // verify SSL certificates
+ $this->setOption(CURLOPT_SSL_VERIFYPEER, true);
+ // should curl_exec return response, not print it on stdout
+ $this->setOption(CURLOPT_RETURNTRANSFER, true);
+ // should not include headers in response
+ $this->setOption(CURLOPT_HEADER, 0);
+ }
+
+ /**
+ * @return string
+ */
+ public function call($method, $url, $payload = '') {
+ if ($method == "POST") {
+ return $this->post($url, $payload);
+ } else if ($method == "PUT") {
+ return $this->put($url, $payload);
+ }
+ return $this->get($url);
+ }
+
+ /**
+ * @return string
+ */
+ public function get($url) {
+ return $this->exec($url);
+ }
+
+ /**
+ * @return string
+ */
+ public function post($url, $data) {
+ $this->setOption(CURLOPT_POST, 1);
+ $this->setOption(CURLOPT_POSTFIELDS, $data);
+ return $this->exec($url);
+ }
+
+ /**
+ * @return string
+ */
+ public function put($url, $data) {
+ $this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');
+ $this->setOption(CURLOPT_POSTFIELDS, $data);
+ return $this->exec($url);
+ }
+
+ /**
+ * @return string
+ */
+ public function delete($url) {
+ $this->setOption(CURLOPT_CUSTOMREQUEST, 'DELETE');
+ return $this->exec($url);
+ }
+
+ /**
+ * @return string
+ */
+ private function exec($url) {
+ $this->setOption(CURLOPT_URL, $this->host . $url);
+ $this->setOption(CURLOPT_HTTPHEADER, $this->headers);
+
+ $curl = curl_init();
+ foreach ($this->options as $key => $value) {
+ curl_setopt($curl, $key, $value);
+ }
+
+ $response = curl_exec($curl);
+ if (!$response) {
+ $error = curl_error($curl);
+ curl_close($curl);
+ throw new PCurlException($error);
+ }
+ curl_close($curl);
+ return $response;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function url($url) {
+ $this->url = $url;
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function auth($user, $password) {
+ $this->setOption(CURLOPT_USERPWD, $user . ":" . $password);
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function proxy($host, $port, $user = null, $password = null) {
+ $this->setOption(CURLOPT_PROXYTYPE, 'HTTP');
+ $this->setOption(CURLOPT_PROXY, $host);
+ $this->setOption(CURLOPT_PROXYPORT, $port);
+ if ($user && $password) {
+ $this->setOption(CURLOPT_PROXYUSERPWD, $user . ":" . $password);
+ }
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function headers(array $headers) {
+ $this->headers = $headers;
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function header($header) {
+ $this->headers[] = $header;
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function contentType($contentType) {
+ $this->header('Content-Type: ' . $contentType);
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function contentTypeJson() {
+ $this->contentType('application/json');
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function ignoreSSLCertificate($bool = true) {
+ $this->setOption(CURLOPT_SSL_VERIFYPEER, !$bool);
+ return $this;
+ }
+
+ /**
+ * @return PCurl
+ */
+ public function useSSLCertificate($certificatePath) {
+ $this->setOption(CURLOPT_SSL_VERIFYHOST, 2);
+ $this->setOption(CURLOPT_CAINFO, $certificatePath);
+ return $this;
+ }
+
+ public function setOption($optionKey, $optionValue) {
+ $this->options[$optionKey] = $optionValue;
+ }
+
+}
diff --git a/src/PurpleCode/PCurl/PCurlException.php b/src/PurpleCode/PCurl/PCurlException.php
new file mode 100644
index 0000000..abed40a
--- /dev/null
+++ b/src/PurpleCode/PCurl/PCurlException.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * PCurl is a REST client libary for PHP.
+ *
+ * See http://github.com/purplecode/php.curl for details.
+ *
+ * This code is licensed for use, modification, and distribution
+ * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
+ */
+
+namespace PurpleCode\PCurl;
+
+class PCurlException extends \Exception {
+
+ public static function getClassName() {
+ return get_called_class();
+ }
+
+ public static function assert($condition, $message, $arguments = array(), $code = 400) {
+ if (!$condition) {
+ $arguments = array_map('json_encode', ArrayUtils::ensureArray($arguments));
+ $arguments = array_merge(array($message), $arguments);
+ $message = call_user_func_array('sprintf', $arguments);
+ $class = self::getClassName();
+ throw new $class($message, $code);
+ }
+ }
+
+ public function __construct($message = "Exception", $code = 400, $previous = null) {
+ parent::__construct($message, $code, $previous);
+ }
+
+ public function ensureArray($item) {
+ if (is_null($item)) {
+ return array();
+ }
+ return is_array($item) ? $item : array($item);
+ }
+
+} \ No newline at end of file