Example #1
0
 private function recognize($body, $is_verbose = true, $rtimeout = 10, $mtimeout = 120, $is_phrase = 0, $is_regsense = 0, $is_numeric = 0, $min_len = 0, $max_len = 0)
 {
     $postData = array('method' => 'base64', 'key' => $this->client->getKey(), 'body' => $body, 'phrase' => $is_phrase, 'regsense' => $is_regsense, 'numeric' => $is_numeric, 'min_len' => $min_len, 'max_len' => $max_len);
     $result = $this->curl->init("http://" . $this->client->getDomain() . "/in.php")->setReturnTransfer(true)->setTimeout(5)->setPostFields($postData)->exec()->getData();
     if (strpos($result, "ERROR") !== false) {
         if ($is_verbose) {
             echo "server returned error: {$result}\n";
         }
         return false;
     } else {
         $ex = explode("|", $result);
         $captchaId = $ex[1];
         if ($is_verbose) {
             echo "captcha sent, got captcha ID {$captchaId}\n";
         }
         $waittime = 0;
         if ($is_verbose) {
             echo "waiting for {$rtimeout} seconds\n";
         }
         sleep($rtimeout);
         $i = 1;
         while (true) {
             $result = $this->curl->init("http://" . $this->client->getDomain() . "/res.php?key=" . $this->client->getKey() . '&action=get&id=' . $captchaId)->exec()->getData();
             if (strpos($result, 'ERROR') !== false) {
                 if ($is_verbose) {
                     echo "server returned error: {$result}\n";
                 }
                 return false;
             }
             if ($result == "CAPCHA_NOT_READY") {
                 if ($is_verbose) {
                     echo "captcha is not ready yet\n";
                 }
                 $waittime += $rtimeout;
                 if ($waittime > $mtimeout) {
                     if ($is_verbose) {
                         echo "timelimit ({$mtimeout}) hit\n";
                     }
                     break;
                 }
                 if ($is_verbose) {
                     echo "waiting for {$rtimeout} seconds\n";
                 }
                 sleep($rtimeout);
             } else {
                 $ex = explode('|', $result);
                 if (trim($ex[0]) == 'OK') {
                     return trim($ex[1]);
                 }
             }
             $i++;
             if ($i > 6) {
                 echo 'captcha too long';
                 return false;
             }
         }
         return false;
     }
 }
Example #2
0
 /**
  * @param $method
  * @param $params
  * @return Client
  * @throws \Exception
  */
 public function send($method, $params)
 {
     $this->setLastQuery(array('method' => $method, 'params' => $params));
     $url = $this->createRequestUrl($method);
     $response = $this->curl->init($url)->setPostFields($params)->exec()->getData();
     if (!is_array($response)) {
         throw new RequestError();
     }
     $check = $this->checkResponse($response);
     if ($check['success'] == true) {
         $this->requestNumber = 0;
         $this->response = $response['response'];
         return $this;
     } else {
         switch ($check['error']) {
             case $this::CAPTCHA_ERROR:
                 // получаем текст капчи
                 $captchaText = $this->anticaptcha->decode($response['error']['captcha_img']);
                 $captcha = array('captcha_key' => $captchaText, 'captcha_sid' => $response['error']['captcha_sid']);
                 echo "Текст капчи: {$captchaText}\n";
                 // отправляем повторный запрос
                 echo "Отправляем повторный запрос\n";
                 $this->requestNumber++;
                 if ($this->requestNumber >= 2) {
                     $this->requestNumber = 0;
                     return null;
                 }
                 return $this->send($method, array_merge($captcha, $params), $token);
                 break;
             case $this::TOKEN_ERROR:
                 // помечаем аккаунт забаненым, если запрос от аккаунта
                 if (!is_null($token)) {
                     $em = $this->getDoctrine()->getManager();
                     $account = $em->getRepository('ProphetzAccountBundle:Account')->findOneBy(array('token' => $token));
                     if (!is_null($account)) {
                         $account->setIsBlocked(true);
                         $em->flush();
                     }
                     $this->requestNumber = 0;
                     return null;
                 }
                 break;
             default:
                 //echo "Не найдено соответствие\n";
                 //var_dump($check);
                 break;
         }
         $this->requestNumber = 0;
         return null;
     }
 }