Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Bitly version-4 #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/Base/ClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,35 @@ private function defaultClient()
*/
public function fetchUrl($url, $parameters = [], $json_formatted = true, $verb = 'get')
{
$full_url = $this->buildUrl($url, $parameters);
if ($verb == 'post') {
$data = json_encode(['long_url' => urldecode($parameters['longUrl'])]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$data,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer ".$parameters['access_token']
),
));

$response = $this->client->{$verb}($full_url);
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$response=json_decode($response);
$response->status_code = $httpcode;
$json_formatted = false;
}else{
$full_url = $this->buildUrl($url, $parameters);
$response = $this->client->{$verb}($full_url);
}

return ($json_formatted) ? $response->json() : $response;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Base/DriversAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function getClient()
*
* @return mixed
*/
public function fetchUrl($url, $parameters)
public function fetchUrl($url, $parameters, $json_formatted = true, $verb = 'get')
{
return $this->clientAdapter->fetchUrl($url, $parameters);
return $this->clientAdapter->fetchUrl($url, $parameters, $json_formatted, $verb);
}
}
12 changes: 10 additions & 2 deletions src/Drivers/Bitly.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ private function setParameters($parameters)
public function shorten($url)
{
// make the API call through the extended client
$response = $this->fetchUrl($this->url(), $this->parameters($url));
$verb = 'get';
if ($this->domain == 'https://api-ssl.bitly.com' && $this->endpoint == '/v4/shorten') {
$verb = 'post';
}
$response = $this->fetchUrl($this->url(), $this->parameters($url), true, $verb);

// read the shorted url from the response object
$shorter_url = $this->parse($response);
Expand Down Expand Up @@ -126,6 +130,10 @@ private function parse($response_object)
$this->validateResponseCode($response_object->status_code);

// return only the short generated url
return $response_object->data->url;
if (isset($response_object->data)) {
return $response_object->data->url;
}else{
return $response_object->link;
}
}
}
1 change: 1 addition & 0 deletions tests/Drivers/BitlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private function driverParameters()
return [
'domain' => 'https://api-ssl.bitly.com',
'endpoint' => '/v3/shorten',
//'endpoint' => '/v4/shorten',
Copy link
Member

@Mulkave Mulkave Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be commented out? or by mistake? @Shivakrishnat

'token' => '1234567890qwertyuiopasdfghjklzxcvbnm',
];
}
Expand Down