summaryrefslogtreecommitdiff
path: root/resolve.php
blob: 47933acf2ed18e5d89a4fab6734f5cfd7b14c241 (plain)
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
<?php
# rate limiting happens in nginx

require_once __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

function resolveURL($url) {

	$client   = new Client();

	try {
		$response = $client->request('HEAD', $url, [ 'on_stats' => function( GuzzleHttp\TransferStats $stats ) use ( &$effectiveURL ){ $effectiveURL = $stats->getEffectiveUri(); }])
			    ->getBody()->getContents();
	} catch(\Exception $e) {
		return $url;
	}

	return $effectiveURL->__toString();
}

function getURL() {
	if ( empty($_REQUEST['url']) ) {
		$data = json_decode(file_get_contents('php://input'), true);
		return $data['url'];
	} else {
		return $_REQUEST['url'];
	}
}

function resolve() {
	return json_encode( ['url' => resolveURL( getURL() ) ]);
}