summaryrefslogtreecommitdiff
path: root/resolve.php
blob: 4df1c8c004a3308efc91dd0c4f669f3b1d14ae13 (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
<?php
# TODO rate limiting
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'];
	}
}

echo json_encode( ['url' => resolveURL( getURL() ) ]);