Advertisement
kura2yamato

run Api

Mar 8th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. function _runApi($url, $parameter = FALSE, $debug = FALSE) {
  3.     $maxTime=10;
  4.         $times = array(microtime());
  5.         $times['begin'] = date("Y-m-d H:i:s");
  6.         $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
  7.         if ($maxTime == null) {
  8.             $maxTime = 10;
  9.         }
  10.  
  11.         if (isset($parameter['maxTime'])) {
  12.             $maxTime = $parameter['maxTime'];
  13.             unset($parameter['maxTime']);
  14.         }
  15.  
  16.         $CI = & get_instance();
  17.         $times['start'] = microtime();
  18.         $dtAPI = array('url' => $url);
  19.  
  20.         if (count($parameter) && $parameter !== FALSE) {
  21.             $logTxt = "func:_runApi| url:{$url}| param:" . http_build_query($parameter, '', '&');
  22.         } else {
  23.             $logTxt = "func:_runApi| url:{$url}";
  24.  
  25.         }
  26.  
  27.         $times[] = microtime();
  28.         $logTxt .= "| max time:{$maxTime}";
  29.         //$parameter[]=array('server'=>$_SERVER);
  30.         $dtAPI['parameter'] = json_encode($parameter);
  31.  
  32.  
  33.         $curl = curl_init();
  34.         $times['init'] = microtime();
  35.  
  36.         curl_setopt($curl, CURLOPT_URL, $url);
  37.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  38.         $times['set opt1'] = microtime();
  39.  
  40.         if (count($parameter) && $parameter !== FALSE) {
  41.             curl_setopt($curl, CURLOPT_POST, true);
  42.             curl_setopt($curl, CURLOPT_TIMEOUT, $maxTime);
  43.             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameter, '', '&'));
  44.             $times['set opt2'] = microtime();
  45.  
  46.  
  47.  
  48.             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  49.             $times['set opt3'] = microtime();
  50.  
  51.  
  52.         } else {
  53.  
  54.         }
  55.  
  56.         curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
  57.         $times['set useragent'] = microtime();
  58.  
  59.         $raw_response = $response = curl_exec($curl);
  60.         $times['execute'] = microtime();
  61.  
  62.         if (0 != curl_errno($curl)) {
  63.             $response = new stdclass();
  64.             $response->code = '500';
  65.             $response->message = curl_error($curl);
  66.             $response->maxTime = $maxTime;
  67.             $times['error 1'] = microtime();
  68.         } else {
  69.             $response0 = $response;
  70.  
  71.             $response = json_decode($response, 1);
  72.             if (!is_array($response)) {
  73.                 $response = $response0;
  74.                 $times['error 2'] = microtime();
  75.             } else {
  76.  
  77.             }
  78.         }
  79.  
  80.         curl_close($curl);
  81.         $times['close'] = microtime();
  82.  
  83.         if (!isset($response0)) {
  84.             $response0 = '?';
  85.         }
  86.  
  87.         $times['end'] = date("Y-m-d H:i:s");
  88.  
  89.         if ($debug) {
  90.             $response = array(
  91.                 'parameters' => $parameter,
  92.                 'url' => $url,
  93.                 'time' => $times,
  94.                 'response' => $response,
  95.                 'raw' => $raw_response
  96.             );
  97.         }
  98.  
  99.         return $response;
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement