Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function _runApi($url, $parameter = FALSE, $debug = FALSE) {
- $maxTime=10;
- $times = array(microtime());
- $times['begin'] = date("Y-m-d H:i:s");
- $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';
- if ($maxTime == null) {
- $maxTime = 10;
- }
- if (isset($parameter['maxTime'])) {
- $maxTime = $parameter['maxTime'];
- unset($parameter['maxTime']);
- }
- $CI = & get_instance();
- $times['start'] = microtime();
- $dtAPI = array('url' => $url);
- if (count($parameter) && $parameter !== FALSE) {
- $logTxt = "func:_runApi| url:{$url}| param:" . http_build_query($parameter, '', '&');
- } else {
- $logTxt = "func:_runApi| url:{$url}";
- }
- $times[] = microtime();
- $logTxt .= "| max time:{$maxTime}";
- //$parameter[]=array('server'=>$_SERVER);
- $dtAPI['parameter'] = json_encode($parameter);
- $curl = curl_init();
- $times['init'] = microtime();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $times['set opt1'] = microtime();
- if (count($parameter) && $parameter !== FALSE) {
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, $maxTime);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameter, '', '&'));
- $times['set opt2'] = microtime();
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
- $times['set opt3'] = microtime();
- } else {
- }
- curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
- $times['set useragent'] = microtime();
- $raw_response = $response = curl_exec($curl);
- $times['execute'] = microtime();
- if (0 != curl_errno($curl)) {
- $response = new stdclass();
- $response->code = '500';
- $response->message = curl_error($curl);
- $response->maxTime = $maxTime;
- $times['error 1'] = microtime();
- } else {
- $response0 = $response;
- $response = json_decode($response, 1);
- if (!is_array($response)) {
- $response = $response0;
- $times['error 2'] = microtime();
- } else {
- }
- }
- curl_close($curl);
- $times['close'] = microtime();
- if (!isset($response0)) {
- $response0 = '?';
- }
- $times['end'] = date("Y-m-d H:i:s");
- if ($debug) {
- $response = array(
- 'parameters' => $parameter,
- 'url' => $url,
- 'time' => $times,
- 'response' => $response,
- 'raw' => $raw_response
- );
- }
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement