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.

66 lines
2.0 KiB

  1. <?php
  2. namespace p3k;
  3. class XRay {
  4. public $http;
  5. public function __construct() {
  6. $this->http = new HTTP('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 p3k/XRay');
  7. }
  8. public function rels($url, $opts=[]) {
  9. $rels = new XRay\Rels($this->http);
  10. return $rels->parse($url, $opts);
  11. }
  12. public function feeds($url, $opts=[]) {
  13. $feeds = new XRay\Feeds($this->http);
  14. return $feeds->find($url, $opts);
  15. }
  16. public function parse($url, $opts_or_body=false, $opts_for_body=[]) {
  17. if(!$opts_or_body || is_array($opts_or_body)) {
  18. $fetch = new XRay\Fetcher($this->http);
  19. $response = $fetch->fetch($url, $opts_or_body);
  20. if(!empty($response['error']))
  21. return $response;
  22. $body = $response['body'];
  23. $url = $response['url'];
  24. $code = $response['code'];
  25. $opts = is_array($opts_or_body) ? $opts_or_body : $opts_for_body;
  26. } else {
  27. $body = $opts_or_body;
  28. $opts = $opts_for_body;
  29. $code = null;
  30. }
  31. $parser = new XRay\Parser($this->http);
  32. $result = $parser->parse([
  33. 'body' => $body,
  34. 'url' => $url,
  35. 'code' => $code,
  36. ], $opts);
  37. if(!isset($opts['include_original']) || !$opts['include_original'])
  38. unset($result['original']);
  39. if(!isset($result['url'])) $result['url'] = $url;
  40. if(!isset($result['code'])) $result['code'] = $code;
  41. if(!isset($result['source-format'])) $result['source-format'] = null;
  42. return $result;
  43. }
  44. public function process($url, $mf2json, $opts=[]) {
  45. $parser = new XRay\Parser($this->http);
  46. $result = $parser->parse([
  47. 'body' => $mf2json,
  48. 'url' => $url,
  49. 'code' => null,
  50. ], $opts);
  51. if(!isset($opts['include_original']) || !$opts['include_original'])
  52. unset($result['original']);
  53. if(!isset($result['url'])) $result['url'] = $url;
  54. if(!isset($result['source-format'])) $result['source-format'] = null;
  55. return $result;
  56. }
  57. }