Example #1
0
 /**
  * Sends single text message.
  *
  * @param string $number recipient's phone number
  * @param string $message the message
  * @param null $signature sender's signature
  * @param null $phoneback sender's phone number
  * @return bool
  * @throws SmsGatewayException
  */
 public function send($number, $message, $signature = null, $phoneback = null)
 {
     $curl = new Curl();
     // Step 2
     $responseStep2 = $curl->sendRequest(self::ENDPOINT . '?page=sendsms', array('phoneno' => $number, 'message' => $message, 'signature' => $signature, 'phoneback' => $phoneback, 'action' => 'verify', 'ads_check1' => 'js_off', 'ads_check2' => 'js_off'));
     $phpsessid = $curl->getHtmlProperty($responseStep2, 'PHPSESSID');
     // Step 3
     $curl->sendRequest(self::ENDPOINT, array('PHPSESSID' => $phpsessid, 'action' => 'confirmbyuser'));
     // Step 4
     $responseStep4 = $curl->sendRequest(self::ENDPOINT, array('operator' => 'donotknow', 'action' => 'confirmprovider'));
     $imagecode = $curl->getHtmlProperty($responseStep4, 'imgcode');
     // Step 5
     $curl->sendRequest(self::ENDPOINT . '?a=sent', array('imgcode' => $imagecode, 'action' => 'useraccepted'));
     $curl->close();
     return true;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function patch($url, $payload, array $options = array())
 {
     return $this->curl->sendRequest($url, 'PATCH', $options, $payload);
 }
 /**
  * {@inheritdoc}
  */
 public function unlink($url, array $options = array())
 {
     return $this->curl->sendRequest($url, 'UNLINK', $options);
 }
Example #4
0
 private function sendApiRequest($method, $data)
 {
     $url = Configure::read('ZzapApi.url') . $method;
     $data['api_key'] = Configure::read('ZzapApi.key');
     $request = json_encode($data);
     // Определяем идет ли это запрос от поискового бота
     $ip = $_SERVER['REMOTE_ADDR'];
     $proxy_type = $this->isBot($ip) ? 'Bot' : 'Site';
     if ($proxy_type == 'Bot' || TEST_ENV) {
         // пытаемся достать инфу из кэша без запроса на API - так быстрее и не нужно юзать прокси
         $_cache = $this->loadModel('ZzapCache')->getCache($method, $request);
         if ($_cache) {
             $this->loadModel('ZzapLog')->clear();
             $this->loadModel('ZzapLog')->save(array('ip_type' => $proxy_type, 'ip' => $ip, 'host' => gethostbyaddr($ip), 'ip_details' => json_encode($_SERVER), 'method' => $method, 'request' => $request, 'response_type' => 'CACHE', 'cache_id' => $_cache['ZzapCache']['id'], 'cache' => $_cache['ZzapCache']['response']));
             return json_decode($_cache['ZzapCache']['response'], true);
         }
     }
     $curl = new Curl($url);
     $curl->setParams($data)->setMethod(Curl::POST)->setFormat(Curl::JSON);
     // этого уже достаточно чтобы отправить запрос
     // если бот - перенаправляем на др.прокси-сервера для ботов - снимаем нагрузку с прокси для сайта
     $proxy = $this->loadModel('ProxyUse')->getProxy($proxy_type);
     $this->loadModel('ProxyUse')->useProxy($proxy['ProxyUse']['host']);
     $curl->setOption(CURLOPT_PROXY, $proxy['ProxyUse']['host'])->setOption(CURLOPT_PROXYUSERPWD, $proxy['ProxyUse']['login'] . ':' . $proxy['ProxyUse']['password']);
     $response = $_response = '';
     $responseType = 'OK';
     try {
         // перед запросом - логируем
         $this->writeLog('REQUEST', "PROXY: {$proxy['ProxyUse']['host']} URL: {$url}; DATA: {$request}");
         $response = $_response = $curl->sendRequest();
         // логируем сразу после запроса
         $this->writeLog('RESPONSE', "PROXY: {$proxy['ProxyUse']['host']} DATA: {$_response}");
     } catch (Exception $e) {
         // отдельно логируем ошибки Curl
         $status = json_encode($curl->getStatus());
         $this->writeLog('ERROR', "PROXY: {$proxy['ProxyUse']['host']} STATUS: {$status}");
         $responseType = 'ERROR';
     }
     $cache_id = null;
     $cache = '';
     $e = null;
     try {
         $response = json_decode($response, true);
         if (!$response || !isset($response['d'])) {
             throw new Exception(__('API Server error'));
         }
         $content = json_decode($response['d'], true);
         if (!isset($content['table']) || $content['error']) {
             throw new Exception(__('API Server response error: %s', $content['error']));
         }
         // если все хорошо - сохраняем ответ в кэше
         $this->loadModel('ZzapCache')->setCache($method, $request, $response['d']);
     } catch (Exception $e) {
         if ($responseType == 'OK') {
             // была ошибка ответа
             $responseType = 'RESPONSE_ERROR';
         }
         // пытаемся достать ответ из кэша
         $_cache = $this->loadModel('ZzapCache')->getCache($method, $request);
         if ($_cache) {
             $cache_id = $_cache['ZzapCache']['id'];
             $cache = $_cache['ZzapCache']['response'];
             $this->writeLog('LOAD CACHE', "PROXY: {$proxy['ProxyUse']['host']} DATA: {$cache}");
             $content = json_decode($cache, true);
             $e = null;
             // сбрасываем ошибку - мы восттановили инфу из кэша
         } else {
             $content = array();
         }
     }
     // Логируем всю инфу для статистики
     $this->loadModel('ZzapLog')->clear();
     $this->loadModel('ZzapLog')->save(array('ip_type' => $proxy_type, 'ip' => $ip, 'host' => gethostbyaddr($ip), 'ip_details' => json_encode($_SERVER), 'proxy_used' => $proxy['ProxyUse']['host'], 'method' => $method, 'request' => $request, 'response_type' => $responseType, 'response_status' => json_encode($curl->getStatus()), 'response' => $_response, 'cache_id' => $cache_id, 'cache' => $cache));
     if ($e) {
         throw $e;
         // повторно кидаем ошибку чтоб ее показать
     }
     return $content;
 }
Example #5
0
 /**
  * @expectedException \Trivago\Tas\HttpHandler\HttpException
  * @expectedExceptionCode 7
  */
 public function test_server_does_not_exist()
 {
     $uri = sprintf('http://%s:%d/test?send_cookie=true', WEB_SERVER_HOST, 9999);
     $curl = new Curl();
     $curl->sendRequest(new HttpRequest($uri, 'GET', []));
 }