diff options
author | purplecode <niespammnie@gmail.com> | 2013-12-08 12:45:51 +0100 |
---|---|---|
committer | purplecode <niespammnie@gmail.com> | 2013-12-08 12:45:51 +0100 |
commit | b3dd84112bc841f071d4df0345cef5e8194fc148 (patch) | |
tree | e5222ffab662479aa025fc6e03ae3d1662395eb6 /src/PurpleCode/PCurl/PCurl.php | |
parent | a55106c4782fc7b503e150d3a3a7cec1faaacf7b (diff) | |
download | php.curl-b3dd84112bc841f071d4df0345cef5e8194fc148.zip php.curl-b3dd84112bc841f071d4df0345cef5e8194fc148.tar.gz php.curl-b3dd84112bc841f071d4df0345cef5e8194fc148.tar.bz2 |
namespace fix
Diffstat (limited to 'src/PurpleCode/PCurl/PCurl.php')
-rw-r--r-- | src/PurpleCode/PCurl/PCurl.php | 193 |
1 files changed, 193 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;
+ }
+
+}
|