diff --git a/src/Services/Bitly.php b/src/Services/Bitly.php index a73eebd..8f74a7c 100644 --- a/src/Services/Bitly.php +++ b/src/Services/Bitly.php @@ -35,29 +35,51 @@ public function bitly($longUrl, $withProtocol = true) $this->validation(func_get_args()[0]); $response = $this->client->request('POST', config('shortlink.bitly.url') . '/shorten', [ - 'query' => [ - 'longUrl' => $longUrl, - 'access_token' => config('shortlink.bitly.key'), + 'headers' => [ + 'Authorization' => 'Bearer '.config('shortlink.bitly.key'), + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'long_url' => $longUrl, ], ]); $result = json_decode($response->getBody()); - - switch ($result->status_code) { + $status_code = $response->getStatusCode(); + $status_text = $response->getReasonPhrase(); + + switch ($status_code) { case '200': if ($withProtocol == false) { - $shortLink = parse_url($result->data->url); + $bitly = $result->id; + } else { + $bitly = $result->link; + } - return $shortLink['host'] . $shortLink['path']; + return $bitly; + break; + case '201': + if ($withProtocol == false) { + $bitly = $result->id; + } else { + $bitly = $result->link; } - return $result->data->url; + return $bitly; break; + case '400': + throw new InvalidApiResponseException("Response $status_code: $status_text"); + case '403': + throw new InvalidApiResponseException("Response $status_code: $status_text"); + case '417': + throw new InvalidApiResponseException("Response $status_code: $status_text"); + case '422': + throw new InvalidApiResponseException("Response $status_code: $status_text"); case '500': - throw new InvalidApiResponseException("Response $result->status_code: $result->status_txt"); + throw new InvalidApiResponseException("Response $status_code: $status_text"); case '503': - throw new InvalidApiResponseException("Response $result->status_code: $result->status_txt"); - } + throw new InvalidApiResponseException("Response $status_code: $status_text"); + } } /** @@ -71,16 +93,19 @@ public function expand($shortUrl) { $this->exceptions(); - $response = $this->client->request('GET', config('shortlink.bitly.url') . '/expand', [ - 'query' => [ - 'access_token' => config('shortlink.bitly.key'), - 'shortUrl' => $shortUrl, + $response = $this->client->request('POST', config('shortlink.bitly.url') . '/expand', [ + 'headers' => [ + 'Authorization' => 'Bearer '.config('shortlink.bitly.key'), + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'bitlink_id' => $shortUrl, ], ]); $result = json_decode($response->getBody()); - - return $result->data->expand[0]->long_url; + + return $result->long_url; } /** diff --git a/src/Services/Service.php b/src/Services/Service.php index dbd95b7..b72abe8 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -62,17 +62,8 @@ public function expand($shortUrl) { $url = $this->isProtocol($shortUrl); - $service = parse_url($url); - - switch($service['host']) { - case 'bit.ly': - $bitly = new Bitly(); - return $bitly->expand($url); - break; - default: - throw new InvalidApiResponseException('Sorry, this service is not supported yet.'); - break; - } + $bitly = new Bitly(); + return $bitly->expand($url); } /** @@ -85,8 +76,9 @@ public function isProtocol($shortUrl) { $url = parse_url($shortUrl); - if ( ! isset($url['scheme'])) { - return 'https://' . $shortUrl; + if (isset($url['scheme'])) { + $shortUrl = str_replace('https://', '', $shortUrl); + return $shortUrl; } return $shortUrl; diff --git a/src/config/shortlink.php b/src/config/shortlink.php index 08e0cb9..36fead7 100644 --- a/src/config/shortlink.php +++ b/src/config/shortlink.php @@ -34,7 +34,7 @@ | */ 'bitly' => [ - 'url' => env('SHORTLINK_BITLY_URL', 'https://api-ssl.bitly.com/v3'), + 'url' => env('SHORTLINK_BITLY_URL', 'https://api-ssl.bitly.com/v4'), 'key' => env('SHORTLINK_BITLY_KEY', 'your_bitly_api_key'), ],