Esempio n. 1
0
 /** 
  * Respose A Http Request 
  * 
  * @param string $url 
  * @param array $post 
  * @param string $method 
  * @param bool $returnHeader 
  * @param string $cookie 
  * @param bool $bysocket 
  * @param string $ip 
  * @param integer $timeout 
  * @param bool $block 
  * @return string Response 
  */
 public static function httpRequest($url, $post = '', $method = 'GET', $limit = 0, $returnHeader = FALSE, $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE)
 {
     $return = '';
     $matches = parse_url($url);
     if (strlen($cookie) == 0) {
         $cookie = NetHelper::getCookie();
     }
     !isset($matches['host']) && ($matches['host'] = '');
     !isset($matches['path']) && ($matches['path'] = '');
     !isset($matches['query']) && ($matches['query'] = '');
     !isset($matches['port']) && ($matches['port'] = '');
     $host = $matches['host'];
     $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/';
     $port = !empty($matches['port']) ? $matches['port'] : 80;
     if (strtolower($method) == 'post') {
         $post = (is_array($post) and !empty($post)) ? http_build_query($post) : $post;
         $out = "POST {$path} HTTP/1.0\r\n";
         $out .= "Accept: */*\r\n";
         //$out .= "Referer: $boardurl\r\n";
         $out .= "Accept-Language: zh-cn\r\n";
         $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
         $out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
         $out .= "Host: {$host}\r\n";
         $out .= 'Content-Length: ' . strlen($post) . "\r\n";
         $out .= "Connection: Close\r\n";
         $out .= "Cache-Control: no-cache\r\n";
         $out .= "Cookie: {$cookie}\r\n\r\n";
         $out .= $post;
     } else {
         $out = "GET {$path} HTTP/1.0\r\n";
         $out .= "Accept: */*\r\n";
         //$out .= "Referer: $boardurl\r\n";
         $out .= "Accept-Language: zh-cn\r\n";
         $out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
         $out .= "Host: {$host}\r\n";
         $out .= "Connection: Close\r\n";
         $out .= "Cookie: {$cookie}\r\n\r\n";
     }
     $fp = fsockopen($ip ? $ip : $host, $port, $errno, $errstr, $timeout);
     if (!$fp) {
         return '';
     } else {
         $header = $content = '';
         stream_set_blocking($fp, $block);
         stream_set_timeout($fp, $timeout);
         fwrite($fp, $out);
         $status = stream_get_meta_data($fp);
         if (!$status['timed_out']) {
             //未超时
             while (!feof($fp)) {
                 $header .= $h = fgets($fp);
                 if ($h && ($h == "\r\n" || $h == "\n")) {
                     break;
                 }
             }
             $stop = false;
             while (!feof($fp) && !$stop) {
                 $data = fread($fp, $limit == 0 || $limit > 8192 ? 8192 : $limit);
                 $content .= $data;
                 if ($limit) {
                     $limit -= strlen($data);
                     $stop = $limit <= 0;
                 }
             }
         }
         fclose($fp);
         return $returnHeader ? array($header, $content) : $content;
     }
 }
Esempio n. 2
0
 /**
  * 通过IP地址定位用户所在的城市(注:使用即通的IP地址库)
  *
  * @return  sting/bool      $data, 为返回数据, 失败返回 false
  */
 public static function getCityByIp($ip, $time = 0)
 {
     $serverNum = 2;
     $rand = rand(0, $serverNum - 1);
     $config = Config::getIP('ipagent', $time == 0 ? $rand : $time % $serverNum);
     if (false === $config) {
         self::$errCode = Config::$errCode;
         self::$errMsg = Config::$errMsg;
         return false;
     }
     // 即通省份ID转易迅省份ID
     $_ProvList = array(1 => 11, 2 => 12, 3 => 13, 4 => 14, 5 => 15, 6 => 32, 7 => 34, 8 => 37, 9 => 21, 10 => 22, 11 => 23, 12 => 31, 13 => 33, 14 => 36, 15 => 35, 16 => 42, 17 => 43, 18 => 41, 19 => 44, 20 => 45, 21 => 46, 22 => 50, 23 => 51, 24 => 52, 25 => 53, 26 => 54, 27 => 61, 28 => 62, 29 => 64, 30 => 63, 31 => 65, 32 => 81, 33 => 82, 34 => 71);
     $ipLookUpAsk = pack("NNNNA64", 0, 80, ip2long($ip), 1, 'this is iptest for 51buy!');
     $ipInfo = NetHelper::udpCmd($config['IP'], $config['PORT'], $ipLookUpAsk, true, 0, 100000);
     if (empty($ipInfo)) {
         if ($time < 3) {
             $time++;
             return self::getCityByIp($ip, $time);
         } else {
             self::$errCode = 10617;
             self::$errMsg = 'failed to request ip_agent server: ' . $config['IP'];
             return false;
         }
     }
     $retArray = unpack("Ntype/Nlen/A64info/Nflag/Nidflag/Ncountry/Nprovince/Ncity/Ntown/Naddrtype/Nnetworktype/Nbackbone1/Nbackbone2", $ipInfo);
     if (isset($retArray) && is_array($retArray) && isset($retArray['province']) && isset($retArray['city']) && isset($retArray['town']) && isset($retArray['backbone1'])) {
         $prov = isset($_ProvList[$retArray['province']]) ? $_ProvList[$retArray['province']] : 0;
         $city = $prov . '00';
         $city2 = $retArray['city'];
         $town = $retArray['town'];
         $net = $retArray['backbone1'];
         // 站点定位只用到省份,目前只做ip_agent省份编码的转换,城市默认取省会
         // 即通的IP地址库可以定位 省->市->区 三级,但ID定义与网站端及ERP端不一致,如果后续需要精确定位,
         // 需要ERP侧做更细致的ID映射,目前为站点定位只映射了省份ID
         return array('prov' => $prov, 'city' => $city, 'city2' => $city2, 'town' => $town, 'net' => $net);
     } else {
         return false;
     }
 }
 public function actionAjaxGetStatus()
 {
     $task_idstr = Yii::app()->request->getParam('tasks');
     $user_id = MenuLoader::getUserId();
     $ids = explode(',', $task_idstr);
     foreach ($ids as $k => &$v) {
         $v = '"' . trim($v) . '"';
     }
     $idstr = implode(',', $ids);
     $my_tasks = array();
     $tasks_proccess = array();
     if ($idstr) {
         //Yii::log('task ids:'.$idstr, 'info');
         $criteria = new CDbCriteria();
         $criteria->condition = 'task_id IN (' . $idstr . ') AND user_id=:user_id';
         $criteria->params = array(':user_id' => $user_id);
         $my_tasks = UserTask::model()->findAll($criteria);
         $base_url = TSInterface::get('G3');
         foreach ($my_tasks as &$task) {
             //Yii::log('task id:'.$task->id.'  status:'.$task->status, 'info');
             if ($task->status != 0 && $task->status != 1) {
                 continue;
             }
             $tasks_proccess[$task->id] = array('total' => 0, 'failed' => 0, 'running' => 0, 'finished' => 0);
             $url = $base_url . '?taskId=' . $task->task_id;
             $content = NetHelper::curl_file_get_contents($url);
             Yii::log('TASK_' . $task->task_id . ' URL:' . $url . ' return:' . $content, 'info');
             $data = json_decode($content, true);
             if ($data && $data['success']) {
                 $all_status = 'finished';
                 if (empty($data['data'])) {
                     $all_status = 'running';
                 }
                 foreach ($data['data'] as $item) {
                     $tasks_proccess[$task->id]['total']++;
                     if ($item['status'] == 'failed') {
                         $all_status = 'failed';
                         $tasks_proccess[$task->id]['failed']++;
                         break;
                     }
                     if ($item['status'] == 'running') {
                         $all_status = 'running';
                         $tasks_proccess[$task->id]['running']++;
                         break;
                     }
                     if ($item['status'] == 'finished') {
                         $tasks_proccess[$task->id]['finished']++;
                         //break;
                     }
                 }
                 if ($all_status == 'running') {
                     $task->status = 1;
                 }
                 if ($all_status == 'failed') {
                     $task->status = -1;
                 }
                 if ($all_status == 'finished') {
                     $task->status = 2;
                 }
                 $task->save();
                 $updateArr = array('status' => $task->status);
                 if ($task->status == 2 || $task->status == -1) {
                     $updateArr['task_result'] = $content;
                     $updateArr['finish_time'] = time();
                 }
                 Yii::log('UPDATE TASK_' . $task->id . ' update_info:' . json_encode($updateArr), 'info');
                 Task::model()->updateAll($updateArr, 'task_id=:task_id', array(':task_id' => $task->task_id));
             }
         }
     }
     $retArr = array();
     foreach ($my_tasks as $item) {
         //Yii::log('export task id:'.$item->id.'  status:'.$item->status, 'info');
         $retArr[$item->id] = $item->status;
     }
     echo json_encode($retArr);
 }