예제 #1
0
 public static function run($para, $usetoken = true)
 {
     self::init();
     $ch = curl_init();
     if (isset($para[self::XII_PARAMS_USERAGENT]) && !empty($para[self::XII_PARAMS_USERAGENT])) {
         curl_setopt($ch, CURLOPT_USERAGENT, $para[self::XII_PARAMS_USERAGENT]);
     } else {
         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
     }
     if (isset($para[self::XII_PARAMS_URL]) && !empty($para[self::XII_PARAMS_URL])) {
         curl_setopt($ch, CURLOPT_URL, $para[self::XII_PARAMS_URL]);
     } else {
         return ['errorCode' => 0, 'errorMsg' => self::XII_PARAMS_URL . ' is null!'];
     }
     if (isset($para[self::XII_PARAMS_REF_URL]) && !empty($para[self::XII_PARAMS_REF_URL])) {
         curl_setopt($ch, CURLOPT_REFERER, $para[self::XII_PARAMS_REF_URL]);
     }
     if (!isset($para[self::XII_PARAMS_DATA])) {
         if (!self::$_allowEmptyData) {
             return ['errorCode' => 0, 'errorMsg' => self::XII_PARAMS_DATA . ' is not find!'];
         }
         $para[self::XII_PARAMS_DATA] = [];
     } else {
         if (!is_array($para[self::XII_PARAMS_DATA])) {
             return ['errorCode' => 0, 'errorMsg' => self::XII_PARAMS_DATA . ' must be array!'];
         }
     }
     if ($usetoken) {
         $token = XiiToken::accessApi();
         $para[self::XII_PARAMS_DATA] = array_merge($para[self::XII_PARAMS_DATA], $token);
     }
     if (count($para[self::XII_PARAMS_DATA]) > 0) {
         if (isset($para[self::XII_PARAMS_METHOD]) && !empty($para[self::XII_PARAMS_METHOD]) && in_array(strtoupper($para[self::XII_PARAMS_METHOD]), array('PUT', 'DELETE', 'POST'))) {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($para[self::XII_PARAMS_METHOD]));
             //curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: " . strtoupper($para[self::XII_PARAMS_METHOD])));
             $para[self::XII_PARAMS_DATA] = http_build_query($para[self::XII_PARAMS_DATA]);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $para[self::XII_PARAMS_DATA]);
         } else {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
             //curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: POST"));
             $para[self::XII_PARAMS_DATA] = http_build_query($para[self::XII_PARAMS_DATA]);
             curl_setopt($ch, CURLOPT_URL, $para['url'] . '?' . $para[self::XII_PARAMS_DATA]);
         }
     } else {
         if (!self::$_allowEmptyData) {
             return ['errorCode' => 0, 'errorMsg' => self::XII_PARAMS_DATA . ' is null!'];
         }
     }
     $timeout = isset($para[self::XII_PARAMS_TIMEOUT]) && !empty($para[self::XII_PARAMS_TIMEOUT]) ? intval($para[self::XII_PARAMS_TIMEOUT]) : 10;
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
     if (isset($para[self::XII_PARAMS_SET]) && !empty($para[self::XII_PARAMS_SET])) {
         foreach ($para[self::XII_PARAMS_SET] as $k => $v) {
             curl_setopt($ch, $k, $v);
         }
     }
     if (isset($para[self::XII_PARAMS_INFO])) {
         $result = curl_exec($ch);
         $info = curl_getinfo($ch);
         curl_close($ch);
         return ['errorCode' => 1, 'data' => $info];
     } else {
         $result = curl_exec($ch);
         curl_close($ch);
         if (XiiUtil::isJson($result)) {
             $tmp = Json::decode($result);
             if (isset($tmp['data'][XiiToken::getIndex()])) {
                 if (!XiiToken::verify($tmp['data'])) {
                     return ['errorCode' => 0, 'errorMsg' => 'Response data be modify!'];
                 }
             }
         }
         return ['errorCode' => 1, 'data' => $result];
     }
 }
예제 #2
0
 public function beforeaction($action)
 {
     if ($this->_requestValidSwtich) {
         /*
             API与验证码处于同一YII中,API请求验证码可以这样获取:
             XiiToken::accessApi();
             Array ( [API_TOKEN] => 7923c897b6fcde20380f3e1439262579 )
             如果不是同一YII配置,确保XiiToken设置一致即可
         */
         $valid = XiiToken::verify($this->_requestValidData);
         if ($valid === 0) {
             XiiError::sendError(408);
             Yii::$app->end();
         }
         if ($valid === false) {
             XiiError::sendError(403);
             Yii::$app->end();
         }
     }
     $this->checkModel();
     return true;
 }