summaryrefslogtreecommitdiff
path: root/public/resolve.php
blob: b103009aa7a1e98c9d2ddb5678e0b2375ec77508 (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() ) ]);