Advertisement
GWibisono

curl

Jan 17th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. $maxTime=20;
  3. if ( ! function_exists('_runApi')){
  4.     function _runApi($url, $parameter=array()){
  5.     global $maxTime;
  6.     $CI =& get_instance();
  7.     $dtAPI=array('url'=>$url);
  8.     if(count($parameter)){
  9.         $logTxt="func:_runApi| url:{$url}| param:".http_build_query($parameter,'','&');
  10.         $dtAPI['parameter']=json_encode($parameter);
  11.     }else{
  12.         $logTxt="func:_runApi| url:{$url}";
  13.         $dtAPI['parameter']='-';
  14.     }
  15.         logCreate( 'API: '.$logTxt);
  16.        
  17.     if(count($parameter)){ 
  18.         //logCreate( 'API: '.'param:'.print_r($parameter,1),'debug');      
  19.         logCreate( 'API: '."url:{$url}| param:".print_r($parameter,1),'debug');
  20.     }else{
  21.         logCreate( 'API: param:'.print_r(parse_url($url),1),'debug');
  22.     }
  23.         $curl = curl_init();
  24.          
  25.         curl_setopt($curl, CURLOPT_URL, $url  );
  26.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  27.         if($parameter != '' && count($parameter)!=0 ) {
  28.             curl_setopt($curl, CURLOPT_POST, true);
  29.             curl_setopt($curl, CURLOPT_TIMEOUT, $maxTime);
  30.             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameter,'','&'));
  31.             if( isset($_SERVER['HTTP_USER_AGENT']) ) curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  32.             logCreate('API:POST','info');
  33.         }else{
  34.             logCreate('API:GET','info');
  35.         }
  36.        
  37.         $response = curl_exec($curl);
  38.  
  39.         if (0 != curl_errno($curl)) {
  40.             $response = new stdclass();
  41.             $response->code = '500';
  42.             $response->message = curl_error($curl);
  43.             $dtAPI['response']=json_encode($response );
  44.             $dtAPI['error']=1;
  45.         }
  46.         else{
  47.             $response0 = $response;
  48.             $dtAPI['response']= $response ;
  49.             $dtAPI['error']=0;
  50.             $response = json_decode($response,1);
  51.             if(!is_array($response)){
  52.                 $response=$response0;
  53.                 $dtAPI['error']=1;
  54.             }
  55.             else{
  56.                 $dtAPI['error']=0;
  57.             }
  58.         }
  59.        
  60.         curl_close($curl);
  61.         if(!isset($response0)) $response0='?';
  62.         logCreate( 'API |url:'. $url. "|raw:".(is_array($response)?'total array/obj='.count($response):$response0 ) );
  63.        
  64.         $CI->db->insert('mujur_api',$dtAPI);   
  65.         return $response;
  66.            
  67.     }
  68. } else{}
  69.  
  70. function logCreate($txt='',$type='debug',$target=''){
  71.    return false;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement