Advertisement
gotopa

Curlme.php

Feb 8th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3.   /**
  4.   * Curlme
  5.   *
  6.   * Library untuk mendapatkan/memposting.
  7.   *
  8.   * @package    CodeIgniter
  9.   * @subpackage libraries
  10.   * @category   library
  11.   * @version    0.2
  12.   * @author     Mustopa Amin <mustopaamin@ymail.com>
  13.   * @date       04-12-2015
  14.   */
  15.  
  16. class Curlme {
  17.  
  18.     /**
  19.     * Default options for every request
  20.     * @static
  21.     */
  22.     public static $curlOptions = array();  
  23.  
  24.     /**
  25.      * Send GET request
  26.      * @param string  $url
  27.      * @param mixed[] $data_hash
  28.      * output json or object(default) or real data
  29.      */
  30.     public static function getData($url,$params,$type = 'object')
  31.     {
  32.        
  33.         $ch = curl_init();  
  34.        
  35.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  36.         curl_setopt($ch,CURLOPT_HEADER, false);
  37.  
  38.         $postData = '';
  39.         if($params)
  40.         {
  41.             //create name value pairs seperated by &
  42.             foreach($params as $k => $v)
  43.             {
  44.               $postData .= $k . '='.str_replace(" ","%20",$v).'&';
  45.             }
  46.             $postData = rtrim($postData, '&');
  47.         }
  48.         curl_setopt($ch,CURLOPT_URL,$url.'?'.$postData);
  49.        
  50.         $output=curl_exec($ch);
  51.        
  52.         curl_close($ch);
  53.         if($type == 'object')
  54.         {
  55.             return json_decode($output);
  56.         }
  57.         else
  58.         {
  59.             return $output;
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Send POST request
  65.      * @param string  $url
  66.      * @param mixed[] $data_hash
  67.      * output json or object(default) or real data
  68.      */
  69.     public static function postData($url,$params = array(),$type = 'object')
  70.     {
  71.         $ch = curl_init();  
  72.        
  73.         curl_setopt($ch,CURLOPT_URL,$url);
  74.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  75.         curl_setopt($ch,CURLOPT_HEADER, false);
  76.  
  77.         if($params)
  78.         {
  79.             curl_setopt($ch, CURLOPT_POST,1);
  80.             curl_setopt($ch, CURLOPT_POSTFIELDS, $params);    
  81.         }
  82.        
  83.         $output=curl_exec($ch);
  84.        
  85.         curl_close($ch);
  86.         if($type == 'object')
  87.         {
  88.             return json_decode($output);
  89.         }
  90.         else
  91.         {
  92.             return $output;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Send GET request
  98.      * @param string  $url
  99.      * @param mixed[] $data_hash
  100.      */
  101.     public static function get($url, $data_hash, $type = 'object')
  102.     {
  103.       return self::remoteCall($url, $data_hash, false, $type);
  104.     }
  105.  
  106.     /**
  107.      * Send POST request
  108.      * @param string  $url
  109.      * @param mixed[] $data_hash
  110.      */
  111.     public static function post($url, $data_hash, $type = 'object')
  112.     {
  113.         return self::remoteCall($url, $data_hash, true, $type);
  114.     }
  115.  
  116.     /**
  117.      * Actually send request to API server
  118.      * @param string  $url
  119.      * @param mixed[] $data_hash
  120.      * @param bool    $post
  121.      * @param type    $type
  122.      */
  123.     public static function remoteCall($url, $data_hash, $post = true, $type = 'object')
  124.     {
  125.         $ch = curl_init();
  126.  
  127.         $curl_options = array(
  128.           CURLOPT_URL => $url,
  129.           CURLOPT_HTTPHEADER => array(
  130.             'Content-Type: application/json',
  131.             'Accept: application/json',
  132.           ),
  133.           CURLOPT_RETURNTRANSFER => 1,
  134.         );
  135.  
  136.         // merging with Veritrans_Config::$curlOptions
  137.         if (count(Curlme::$curlOptions)) {
  138.           // We need to combine headers manually, because it's array and it will no be merged
  139.           if (Curlme::$curlOptions[CURLOPT_HTTPHEADER]) {
  140.             $mergedHeders = array_merge($curl_options[CURLOPT_HTTPHEADER], Curlme::$curlOptions[CURLOPT_HTTPHEADER]);
  141.             $headerOptions = array( CURLOPT_HTTPHEADER => $mergedHeders );
  142.           } else {
  143.             $mergedHeders = array();
  144.           }
  145.  
  146.           $curl_options = array_replace_recursive($curl_options, Curlme::$curlOptions, $headerOptions);
  147.         }
  148.  
  149.         if ($post) {
  150.           $curl_options[CURLOPT_POST] = 1;
  151.  
  152.           if ($data_hash) {
  153.             $body = json_encode($data_hash);
  154.             $curl_options[CURLOPT_POSTFIELDS] = $body;
  155.           } else {
  156.             $curl_options[CURLOPT_POSTFIELDS] = '';
  157.           }
  158.         }
  159.  
  160.         curl_setopt_array($ch, $curl_options);
  161.        
  162.         $result = curl_exec($ch);
  163.         // curl_close($ch);
  164.  
  165.         if ($result === FALSE) {
  166.           throw new Exception('CURL Error: ' . curl_error($ch), curl_errno($ch));
  167.         }
  168.         else {
  169.             if($type == 'object')
  170.             {
  171.               $result_array = json_decode($result);
  172.             }
  173.             else
  174.             {
  175.                 $result_array = $result;
  176.             }
  177.           return $result_array;
  178.         }
  179.     }
  180.  
  181.     function counter()
  182.     {
  183.         $ci =& get_instance();
  184.         $ci->load->helper('file');
  185.         $file= (dirname(__FILE__) . "/data_pengunjung.txt");
  186.         $kunjungan=read_file($file);
  187.         if($kunjungan != 500 )
  188.         {
  189.             $kunjungan  = $kunjungan + 1;
  190.         }
  191.         else
  192.         {
  193.             $kunjungan  = 1;
  194.         }
  195.        
  196.         write_file($file, $kunjungan);
  197.        
  198.         if($kunjungan > 0 && $kunjungan < 10) { $kunjungan = "00".$kunjungan;   }
  199.         elseif($kunjungan > 9 && $kunjungan < 100) {    $kunjungan = "0".$kunjungan;    }
  200.         else { $kunjungan = $kunjungan ; }
  201.                
  202.         return  $kunjungan;
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement