-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.expander.php
More file actions
38 lines (35 loc) · 1.02 KB
/
method.expander.php
File metadata and controls
38 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
if (!function_exists('expand')) {
/**
* Expander for tiny url.
* @param string $url
* @return string
*
* @example expand('https://tinyurl.com/ycrtdwxa');
*/
function expand(string $url): string
{
$curl = null;
$response = '';
$matches = '';
$expanded = array();
if (!function_exists('curl_version')) {
trigger_error('cURL is not found!');
exit();
} else {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
$response = curl_exec($curl);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($curl);
if (!empty($matches[1][0])) {
$expanded = $matches[1][0];
} else {
$expanded = $url;
}
}
return($expanded);
}
}