apipage/src/actions.php

58 lines
1.2 KiB
PHP
Raw Normal View History

2019-06-26 14:41:18 +02:00
<?php
class Action
{
/**
* Home action.
*
* @return string
*/
public function home():string
{
return response()
->httpCode(200)
->json([]);
}
/**
* Parser action.
*
* @param string $parser
*
* @return string
*/
public function parse(string $parser):string
{
$url = input()->get('url')->value;
if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
return response()
->httpCode(400)
->json(['error' => 'Invalid URL']);
}
$parsers = [
'graby' => 'graby',
'opengraph' => 'opengraph',
];
if (!isset($parsers[$parser])) {
return response()
->httpCode(400)
->json(['error' => 'Invalid parser']);
}
try {
$result = call_user_func_array($parsers[$parser], [$url]);
return response()
->httpCode(200)
->json($result);
} catch (\Exception $e) {
return response()
->httpCode(500)
->json(['error' => $e->getMessage()]);
}
}
}