Skip to content

Instantly share code, notes, and snippets.

@soknifedev
Created February 6, 2020 19:57
Show Gist options
  • Save soknifedev/32b2c7f638c49c03a555ebd7a3ffc97c to your computer and use it in GitHub Desktop.
Save soknifedev/32b2c7f638c49c03a555ebd7a3ffc97c to your computer and use it in GitHub Desktop.
Gets bitly clicks with posibility of filtering with start and/or end dates.
<?php
header('content-type: application/json');
function error() {
$args = func_get_args();
$response = ['success' => false];
$response['message'] = (String) array_shift($args);
$response['data'] = $args;
die(json_encode($response, JSON_PRETTY_PRINT));
}
function success() {
$args = func_get_args();
$response = ['success' => true];
$response['message'] = (String) array_shift($args);
$response['data'] = $args;
die(json_encode($response, JSON_PRETTY_PRINT));
}
function do_curl($opts) {
$ch = curl_init();
foreach ($opts as $opt => $opt_val) {
curl_setopt($ch, $opt, $opt_val);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function parse_bitly($str) {
if(preg_match('/(bit\.ly\/.*)/iu', $str, $m)) return $m[1];
return 'bit.ly/'.$str;
}
$bitly_credentials = ['user', 'pass']; // username, password
$bitly_id = parse_bitly($_REQUEST['id']);
$bitly_options = [
'units' => -1, // alwas get all records from bitly
'unit' => $_REQUEST['unit'] ? $_REQUEST['unit'] : 'day' // custom unit can be specified
];
$authRequest = [
CURLOPT_URL => 'https://api-ssl.bitly.com/oauth/access_token',
CURLOPT_USERPWD => implode(':', $bitly_credentials),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, // disables SSL verification
CURLOPT_SSL_VERIFYHOST => false, // disables SSL verification
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query([
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => 'https://apps.wortise.com/bitly/callback'
])
];
// I specify it manually because of the bitly rate limits
$accessToken = 'xx97c668aae99b9xxxc17cbb5cc97ba0f2e9fc1x'; //do_curl($authRequest); // valid bitly tokens appears to be of 40 chars
if(strlen($accessToken) > 40 || strlen($accessToken) < 40) {
error('Cannot authenticate to bitly', ['auth_result' => $accessToken]);
}
$authorization = 'Authorization: Bearer '.$accessToken;
$clicksRequest = [
CURLOPT_URL => 'https://api-ssl.bitly.com/v4/bitlinks/'.$bitly_id.'/clicks?'.http_build_query($bitly_options),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array($authorization), // injects bitly auth into header
CURLOPT_SSL_VERIFYPEER => false, // disables SSL verification
CURLOPT_SSL_VERIFYHOST => false, // disables SSL verification
];
$result = json_decode(do_curl($clicksRequest)); // valid bitly tokens appears to be of 40 chars
if($result->message) {
error($result->message, ['id' => $bitly_id]);
}
$clicks = $result->link_clicks;
$start = strtotime($_REQUEST['start']);
$end = strtotime($_REQUEST['end']);
if(isset($start) && isset($end) && $end < $start) {
error('Invalid end date, it must be higher than the start date');
}
if(isset($start) && isset($end) && $start > $end) {
error('Invalid start date, it must be lower than the end date');
}
if(isset($start) || isset($end)) {
foreach($clicks as $k => $click) {
$click_ts = strtotime($click->date);
if( ! (( $click_ts >= $start || !isset($start) ) && ( $click_ts <= $end || !isset($end) )) ) {
unset($clicks[$k]);
}
}
}
$clicks = array_values($clicks);
success('clicks retrieved with success', ['clicks' => $clicks, 'options' => $bitly_options, 'id' => $bitly_id, 'request' => $_REQUEST]/*, ['res' => $result]*/);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment