Advertisement
kura2yamato

run api

Jan 24th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1.     function _runApi($url, $parameter = array(), $debug = FALSE) {
  2.         global $maxTime;
  3.         if ($maxTime == null){
  4.             $maxTime = 10;
  5.         }
  6.         if (isset($parameter['maxTime'])){
  7.             $maxTime = $parameter['maxTime'];
  8.         }
  9.         //log_info_table('run_api',array('old',$url,count($parameter) ));
  10.  
  11.         $CI = & get_instance();
  12.         $dtAPI = array('url' => $url);
  13.         if (count($parameter)) {
  14.             $logTxt = "func:_runApi| url:{$url}| param:" . http_build_query($parameter, '', '&');
  15.         } else {
  16.             $logTxt = "func:_runApi| url:{$url}";
  17.             $parameter['info'] = 'no post';
  18.         }
  19.         //$parameter[]=array('server'=>$_SERVER);
  20.         $dtAPI['parameter'] = json_encode($parameter);
  21.         logCreate('API: ' . $logTxt);
  22.  
  23.         if (count($parameter)) {
  24.             logCreate('API: ' . "url:{$url}| param:\n" . print_r($parameter, 1), 'debug');
  25.         } else {
  26.             logCreate('API: param:' . print_r(parse_url($url), 1), 'debug');
  27.         }
  28.         $curl = curl_init();
  29.  
  30.         curl_setopt($curl, CURLOPT_URL, $url);
  31.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  32.         if ($parameter != '' && count($parameter) != 0) {
  33.             curl_setopt($curl, CURLOPT_POST, true);
  34.             curl_setopt($curl, CURLOPT_TIMEOUT, $maxTime);
  35.             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameter, '', '&'));
  36.             //curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/path/to/file.ext');
  37.             //if( isset($_SERVER['HTTP_USER_AGENT']) )
  38.             //  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  39.  
  40.             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  41.             logCreate('API:POST', 'info');
  42.         } else {
  43.             logCreate('API:GET', 'info');
  44.         }
  45.        
  46.         curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  47.         $response = curl_exec($curl);
  48.  
  49.         if (0 != curl_errno($curl)) {
  50.             $response = new stdclass();
  51.             $response->code = '500';
  52.             $response->message = curl_error($curl);
  53.             $response->maxTime = $maxTime;
  54.             $dtAPI['response'] = json_encode($response);
  55.             $dtAPI['error'] = 1;
  56.         } else {
  57.             $response0 = $response;
  58.             $dtAPI['response'] = $response;
  59.             $dtAPI['error'] = 0;
  60.             $response = json_decode($response, 1);
  61.             if (!is_array($response)) {
  62.                 $response = $response0;
  63.                 $dtAPI['error'] = 1;
  64.             } else {
  65.                 $dtAPI['error'] = 0;
  66.             }
  67.         }
  68.  
  69.         curl_close($curl);
  70.         if (!isset($response0)){
  71.             $response0 = '?';
  72.         }
  73.        
  74.         logCreate('helper| API| url:' . $url . "|raw:" . (is_array($response) ? 'total array/obj=' . count($response) : $response0 ));
  75.  
  76.         return $debug ? array($url, $parameter, 'response' => $response,'raw'=>$response0) : $response;
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement