58 行
1.2 KiB
PHP
58 行
1.2 KiB
PHP
<?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()]);
|
|
}
|
|
}
|
|
}
|