Skip to content

Commit

Permalink
Merge pull request #1 from div-art/bitly_v4
Browse files Browse the repository at this point in the history
Update package for Bitly v4 API
  • Loading branch information
IevgeniiDomanskyi committed Sep 27, 2018
2 parents d7cbf73 + 2bd6531 commit c4811a3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
59 changes: 42 additions & 17 deletions src/Services/Bitly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

/**
Expand All @@ -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;
}

/**
Expand Down
18 changes: 5 additions & 13 deletions src/Services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/config/shortlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],

Expand Down

0 comments on commit c4811a3

Please sign in to comment.