示例#1
0
 public function shorten($longUrl, $returnFullReponse = false)
 {
     if (!Validate::isUrl($longUrl)) {
         throw new \Exception('Url parameter must be a valid url');
     }
     if (!is_bool($returnFullReponse)) {
         throw new \Exception('returnFullReponse parameter must be an boolean');
     }
     $curl = new Curl($this->getApiUrl() . 'shorten');
     $curl->addArgument('version', $this->getApiVersion(), 'GET');
     $curl->addArgument('longUrl', $longUrl, 'GET');
     $curl->addArgument('login', $this->getApiLogin(), 'GET');
     $curl->addArgument('apiKey', $this->getApiKey(), 'GET');
     $curl->execute(true);
     $reponse = json_decode($curl->getResponse());
     if ($returnFullReponse) {
         return $reponse;
     } else {
         if ($reponse->errorCode != 0) {
             throw new \Exception('Bit.Ly shorten url error, code : "' . $reponse->errorCode . '" and message : "' . $reponse->errorMessage . '"');
         }
         if ($reponse->statusCode == 'OK') {
             Logger::getInstance()->debug('Bit.Ly shorten url : "' . $longUrl . '" result is : "' . $reponse->results->{$longUrl}->shortUrl . '"');
             return $reponse->results->{$longUrl}->shortUrl;
         }
         return false;
     }
 }
示例#2
0
 public function shorten($longUrl, $returnFullReponse = false)
 {
     if (!Validate::isUrl($longUrl)) {
         throw new \Exception('longUrl parameter must be a valid url');
     }
     if (!is_bool($returnFullReponse)) {
         throw new \Exception('returnFullReponse parameter must be an boolean');
     }
     $curl = new Curl($this->getApiUrl());
     if ($this->getApiKey()) {
         $curl->addArgument('key', $this->getApiKey(), 'GET');
     }
     $curl->addArgument('longUrl', $longUrl, 'POST');
     $curl->setCurlOpt(CURLOPT_HTTPHEADER, array('Content-type: application/json'));
     $curl->execute(true, false, false, true);
     $reponse = json_decode($curl->getResponse());
     if ($returnFullReponse) {
         return $reponse;
     } else {
         if (isset($reponse->error)) {
             Logger::getInstance()->debug('Goo.gl shorten url error, code : "' . $reponse->error->code . '" and message : "' . $reponse->error->message . '"');
             return false;
         } else {
             Logger::getInstance()->debug('Goo.gl shorten url : "' . $longUrl . '" result is : "' . $reponse->id . '"');
             return $reponse->id;
         }
     }
 }
示例#3
0
 public static function getServerResponse($url = null, $format = false, $onlyResponseCode = false, $checkIfSent = false)
 {
     if (!is_bool($checkIfSent)) {
         throw new \Exception('CheckIfSent parameter must be an boolean');
     }
     if (!is_bool($onlyResponseCode)) {
         throw new \Exception('onlyResponseCode parameter must be an boolean');
     }
     if ($onlyResponseCode) {
         return self::getHeaderResponseStatusCode();
     }
     if (is_null($url)) {
         $request = Http::getRootRequest();
         $url = Http::isHttps() ? 'https://' . $request : 'http://' . $request;
     }
     if (!Validate::isUrl($url)) {
         throw new \Exception('Url must be a valid url');
     }
     if (!is_bool($format)) {
         throw new \Exception('Format parameter must be an boolean');
     }
     $response = $checkIfSent ? headers_sent() ? get_headers($url, $format) : null : get_headers($url, $format);
     if (!$response && !$checkIfSent) {
         throw new \Exception('Error while get headers response status');
     }
     return $response;
 }
示例#4
0
 public function setUrl($url)
 {
     if (!$this->_getCurlInitialized()) {
         throw new \Exception('Curl must be initialized');
     }
     if (!is_string($url)) {
         throw new \Exception('Url parameter must be a string');
     }
     if (!Validate::isUrl($url)) {
         throw new \Exception('Url parameter must be a valid url');
     }
     if (Validate::isUrl($url, true)) {
         if (!$this->getAllowSsl()) {
             throw new \Exception('SSL url cannot be used');
         }
         $this->setCurlOpt(CURLOPT_SSL_VERIFYPEER, false);
         $this->setCurlOpt(CURLOPT_SSL_VERIFYHOST, 2);
     }
     $this->_url = $url;
 }