Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function bitly_shorten($url, $token) {
- $args = [
- 'access_token' => $token,
- 'longUrl' => $url
- ];
- $uri = 'https://api-ssl.bitly.com/v3/shorten?' .
- http_build_query($args, '', '&', PHP_QUERY_RFC3986);
- $output = "";
- try {
- $ch = curl_init($uri);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, 4);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- $output = curl_exec($ch);
- }
- catch (Exception $e) {
- return $url;
- }
- $res = json_decode($output, true);
- if ($res['status_txt'] == 'OK') {
- return $res['data']['url'];
- }
- return $url;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement