You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class Feeds {
  5. public $http;
  6. private $_pretty = false;
  7. public function __construct() {
  8. $this->http = new p3k\HTTP();
  9. }
  10. private function respond(Response $response, $code, $params, $headers=[]) {
  11. $response->setStatusCode($code);
  12. foreach($headers as $k=>$v) {
  13. $response->headers->set($k, $v);
  14. }
  15. $response->headers->set('Content-Type', 'application/json');
  16. $opts = JSON_UNESCAPED_SLASHES;
  17. if($this->_pretty) $opts += JSON_PRETTY_PRINT;
  18. $response->setContent(json_encode($params, $opts)."\n");
  19. return $response;
  20. }
  21. public function find(Request $request, Response $response) {
  22. $opts = [];
  23. if($request->get('timeout')) {
  24. $opts['timeout'] = $request->get('timeout');
  25. }
  26. if($request->get('max_redirects')) {
  27. $opts['max_redirects'] = (int)$request->get('max_redirects');
  28. }
  29. if($request->get('pretty')) {
  30. $this->_pretty = true;
  31. }
  32. $url = $request->get('url');
  33. if(!$url) {
  34. return $this->respond($response, 400, [
  35. 'error' => 'missing_url',
  36. 'error_description' => 'Provide a URL to fetch'
  37. ]);
  38. }
  39. $xray = new p3k\XRay();
  40. $xray->http = $this->http;
  41. $res = $xray->feeds($url, $opts);
  42. return $this->respond($response, !empty($res['error']) ? 400 : 200, $res);
  43. }
  44. }