Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Curl {
- protected $_ci;
- protected $response = "";
- protected $session;
- protected $url;
- protected $options = [];
- protected $headers = [];
- public $error_code;
- public $error_string;
- public $info;
- function __construct($url="") {
- $url AND $this->create($url);
- }
- public function __call($method, $arguments) {
- if(in_array($method, ["simple_get", "simple_post", "simple_put", "simple_delete", "simple_patch"])) {
- $verb = str_replace("simple_", "", $method);
- array_unshift($arguments, $verb);
- return call_user_func_array([$this, "_simple_call"], $arguments);
- }
- }
- public function _simple_call($method, $url, $params = [], $options = []) {
- if($method === 'get') {
- $this->create($url . ($params ? "?" . http_build_query($params, null, "&") : ""));
- } else {
- $this->create($url);
- $this->{$method}($params);
- }
- $this->options($options);
- return $this->execute();
- }
- public function simple_ftp_get($url, $file_path, $username="", $password="") {
- if(!preg_match('!^(ftp|sftp)://! i', $url)) {
- $url = "ftp://" . $url;
- }
- if($username != "") {
- $auth_string = $username;
- if($password != "") {
- $auth_string .= ":" . $password;
- }
- $url = str_replace("://", "://" . $auth_string . "@", $url);
- }
- $url .= $file_path;
- $this->option(CURLOPT_BINARYTRANSFER, true);
- $this->option(CURLOPT_VERBOSE, true);
- return $this->execute();
- }
- public function post($params=[], $options=[]) {
- if(is_array($params)) {
- $params = http_build_query($params, null, "&");
- }
- $this->options($options);
- $this->http_method("post");
- $this->option(CURLOPT_POST, true);
- $this->option(CURLOPT_POSTFIELDS, $params);
- }
- public function put($params=[], $options=[]) {
- if(is_array($params)) {
- $params = http_build_query($params, null, "&");
- }
- $this->options($options);
- $this->http_method("put");
- $this->option(CURLOPT_POSTFIELDS, $params);
- $this->option(CURLOPT_HTTPHEADER, ["X-HTTP-Method-Override: PUT"]);
- }
- public function patch($params=[], $options=[]) {
- if(is_array($params)) {
- $params = http_build_query($params, null, "&");
- }
- $this->options($options);
- $this->http_method("patch");
- $this->option(CURLOPT_POSTFIELDS, $params);
- $this->option(CURLOPT_HTTPHEADER, ["X-HTTP-Method-Override: PATCH"]);
- }
- public function delete($params, $options=[]) {
- if(is_array($params)) {
- $params = http_build_query($params, null, "&");
- }
- $this->options($options);
- $this->http_method("delete");
- $this->option(CURLOPT_POSTFIELDS, $params);
- }
- public function set_cookies($params=[]) {
- if(is_array($params)) {
- $params = http_build_query($params, null, "&");
- }
- $this->option(CURLOPT_COOKIE, $params);
- return $this;
- }
- public function http_header($header, $content=null) {
- $this->headers[] = $content ? $header . ": " . $content : $header;
- return $this;
- }
- public function http_method($method) {
- $this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
- return $this;
- }
- public function http_login($username="", $password="", $type="any") {
- $this->option(CURLOPT_HTTPAUTH, constant("CURLAUTH_" . strtoupper($type)));
- $this->option(CURLOPT_USERPWD, $username . ":" . $password);
- return $this;
- }
- public function proxy($url="", $port=80) {
- $this->option(CURLOPT_HTTPPROXYTUNNEL, true);
- $this->option(CURLOPT_PROXY, $url . ":" . $port);
- return $this;
- }
- public function proxy_login($username="", $password="") {
- $this->option(CURLOPT_PROXYUSERPWD, $username . ":" . $password);
- return $this;
- }
- public function ssl($verify_peer=true, $verify_host=2, $path_to_cert=null) {
- if($verify_peer) {
- $this->option(CURLOPT_SSL_VERIFYPEER, true);
- $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
- if(isset($path_to_cert)) {
- $path_to_cert = realpath($path_to_cert);
- $this->option(CURLOPT_CAINFO, $path_to_cert);
- }
- } else {
- $this->option(CURLOPT_SSL_VERIFYPEER, false);
- $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
- }
- return $this;
- }
- public function options($options=[]) {
- foreach($options as $option_code => $option_value) {
- $this->option($option_code, $option_value);
- }
- curl_setopt_array($this->session, $this->options);
- return $this;
- }
- public function option($code, $value, $prefix="opt") {
- if(is_string($code) && !is_numeric($code)) {
- $code = constant("CURL" . strtoupper($prefix) . "_" . strtoupper($code));
- }
- $this->options[$code] = $value;
- return $this;
- }
- public function create($url) {
- if(!preg_match("!^\w+://! i", $url)) {
- $this->_ci->load->helper("url");
- $url = site_url($url);
- }
- $this->url = $url;
- $this->session = curl_init($this->url);
- return $this;
- }
- public function execute() {
- if(!isset($this->options[CURLOPT_TIMEOUT])) {
- $this->options[CURLOPT_TIMEOUT] = 30;
- }
- if(!isset($this->options[CURLOPT_DNS_USE_GLOBAL_CACHE])) {
- $this->options[CURLOPT_DNS_USE_GLOBAL_CACHE] = true;
- }
- if(!isset($this->options[CURLOPT_FRESH_CONNECT])) {
- $this->options[CURLOPT_FRESH_CONNECT] = true;
- }
- if(!isset($this->options[CURLOPT_FORBID_REUSE])) {
- $this->options[CURLOPT_FORBID_REUSE] = true;
- }
- if(!isset($this->options[CURLOPT_RETURNTRANSFER])) {
- $this->options[CURLOPT_RETURNTRANSFER] = true;
- }
- if(!isset($this->options[CURLOPT_FAILONERROR])) {
- $this->options[CURLOPT_FAILONERROR] = true;
- }
- if(!ini_get("safe_mode") && ! ini_get("open_basedir")) {
- if(!isset($this->options[CURLOPT_FOLLOWLOCATION])) {
- $this->options[CURLOPT_FOLLOWLOCATION] = true;
- }
- }
- if(!empty($this->headers)) {
- $this->option(CURLOPT_HTTPHEADER, $this->headers);
- }
- $this->options();
- $this->response = curl_exec($this->session);
- $this->info = curl_getinfo($this->session);
- if($this->response === false) {
- $errno = curl_errno($this->session);
- $error = curl_error($this->session);
- curl_close($this->session);
- $this->set_defaults();
- $this->error_code = $errno;
- $this->error_string = $error;
- return false;
- } else {
- curl_close($this->session);
- $this->last_response = $this->response;
- $this->set_defaults();
- return $this->last_response;
- }
- }
- public function is_enabled() {
- return function_exists("curl_init");
- }
- public function debug() {
- echo "=============================================<br/>\n";
- echo "<h2>Test Curl</h2>\n";
- echo "=============================================<br/>\n";
- echo "<h3>Respon</h3>\n";
- echo "<code>" . nl2br(htmlentities($this->last_response)) . "</code><br/>\n\n";
- if($this->error_string) {
- echo "=============================================<br/>\n";
- echo "<h3>Err!</h3>";
- echo "<strong>Kode:</strong> " . $this->error_code . "<br/>\n";
- echo "<strong>Pesan:</strong> " . $this->error_string . "<br/>\n";
- }
- echo "=============================================<br/>\n";
- echo "<h3>Informasi</h3>";
- echo "<pre>";
- print_r($this->info);
- echo "</pre>";
- }
- public function debug_request() {
- return ["url" => $this->url];
- }
- function get_string($str) {
- $ci =& get_instance();
- $key = "44zfD5vy360v5Kn29wc5RGZLqeLn1baw";
- return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($str), MCRYPT_MODE_CBC, "abcdefghijklmnopqrstuvwxyz012345"), "\0");
- }
- public function set_defaults() {
- $this->response = "";
- $this->headers = [];
- $this->options = [];
- $this->error_code = null;
- $this->error_string = "";
- $this->session = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement