Advertisement
kura2yamato

buat curl

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