src/includes/classes/Core/Utils/Bitly.php
public function shorten(string $long_url, array $args = []): string
    {
        # Parse incoming arguments.

        if (!$long_url) {
            return ''; // Nothing to do.
        }
        $default_args = [
            'access_token' => $this->App->Config->©bitly['©api_key'],
            'longUrl'      => $long_url, // From above.
        ];
        $args = array_merge($default_args, $args);

        # Already cached this in memory?

        if (!is_null($short_url = &$this->cacheKey(__FUNCTION__, $args))) {
            return $short_url; // Cached this already.
        }
        # Prepare endpoint/request args.

        $endpoint_args = array_merge($args, [
            // Nothing more at this time.
        ]);
        $request_args = [
            'max_con_secs'    => 5,
            'max_stream_secs' => 5,
            'return'          => 'array',
        ];
        $endpoint = 'https://api-ssl.bitly.com/v3/shorten';
        $endpoint = $this->c::addUrlQueryArgs($endpoint_args, $endpoint);

        # Determine sharded cache directory and file.

        $endpoint_sha1         = sha1($endpoint);
        $cache_dir             = $this->cache_dir.'/'.$this->c::sha1ModShardId($endpoint_sha1, true);
        $cache_dir_permissions = $this->App->Config->©fs_permissions['©transient_dirs'];
        $cache_file            = $cache_dir.'/'.$endpoint_sha1;

        # Use cached response if at all possible.

        if (is_file($cache_file)) { // File exists?
            return $short_url = (string) file_get_contents($cache_file);
        }
        # Query for remote response via API endpoint URL.

        $response = $this->c::remoteRequest('GET::'.$endpoint, $request_args);

        # Validate response; no cache on any error.

        if ($response['code'] !== 200) {
            return $long_url; // Do not cache.
        }
        if (!is_object($json = json_decode($response['body']))) {
            return $long_url; // Do not cache.
        }
        if (!isset($json->status_txt) || $json->status_txt !== 'OK') {
            return $long_url; // Do not cache.
        }
        if (!isset($json->data->url) || !is_string($json->data->url)) {
            return $long_url; // Do not cache.
        }
        # Cache and return response.

        if (!is_dir($cache_dir)) {
            mkdir($cache_dir, $cache_dir_permissions, true);
        }
        $short_url = $json->data->url;
        file_put_contents($cache_file, $short_url);

        return $short_url;
    }
public function shorten(string $long_url, array $args = []): string
    {
        # Parse incoming arguments.

        if (!$long_url) {
            return ''; // Nothing to do.
        }
        $default_args = [
            'access_token' => $this->App->Config->©bitly['©api_key'],
            'longUrl'      => $long_url, // From above.
        ];
        $args = array_merge($default_args, $args);

        # Already cached this in memory?

        if (!is_null($short_url = &$this->cacheKey(__FUNCTION__, $args))) {
            return $short_url; // Cached this already.
        }
        # Prepare endpoint/request args.

        $endpoint_args = array_merge($args, [
            // Nothing more at this time.
        ]);
        $request_args = [
            'max_con_secs'    => 5,
            'max_stream_secs' => 5,
            'return'          => 'array',
        ];
        $endpoint = 'https://api-ssl.bitly.com/v3/shorten';
        $endpoint = $this->c::addUrlQueryArgs($endpoint_args, $endpoint);

        # Determine sharded cache directory and file.

        $endpoint_sha1         = sha1($endpoint);
        $cache_dir             = $this->cache_dir.'/'.$this->c::sha1ModShardId($endpoint_sha1, true);
        $cache_dir_permissions = $this->App->Config->©fs_permissions['©transient_dirs'];
        $cache_file            = $cache_dir.'/'.$endpoint_sha1;

        # Use cached response if at all possible.

        if (is_file($cache_file)) { // File exists?
            return $short_url = (string) file_get_contents($cache_file);
        }
        # Query for remote response via API endpoint URL.

        $response = $this->c::remoteRequest('GET::'.$endpoint, $request_args);

        # Validate response; no cache on any error.

        if ($response['code'] !== 200) {
            return $long_url; // Do not cache.
        }
        if (!is_object($json = json_decode($response['body']))) {
            return $long_url; // Do not cache.
        }
        if (!isset($json->status_txt) || $json->status_txt !== 'OK') {
            return $long_url; // Do not cache.
        }
        if (!isset($json->data->url) || !is_string($json->data->url)) {
            return $long_url; // Do not cache.
        }
        # Cache and return response.

        if (!is_dir($cache_dir)) {
            mkdir($cache_dir, $cache_dir_permissions, true);
        }
        $short_url = $json->data->url;
        file_put_contents($cache_file, $short_url);

        return $short_url;
    }
Shorten a URL.

@since 160102 Adding bitly.

@param string $long_url Long URL.
@param array  $args     Any additional args.

@return string Short URL.