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.

398 lines
14 KiB

8 years ago
7 years ago
7 years ago
7 years ago
  1. XRay
  2. ====
  3. XRay parses structured content from a URL.
  4. ## Discovering Content
  5. XRay will parse content in the following formats. First the URL is checked against known services:
  6. * Instagram
  7. * Twitter
  8. * GitHub
  9. * XKCD
  10. * Hackernews
  11. If the contents of the URL is XML or JSON, then XRay will parse the Atom, RSS or JSONFeed formats.
  12. Finally, XRay looks for Microformats on the page and will determine the content from that.
  13. * h-card
  14. * h-entry
  15. * h-event
  16. * h-review
  17. * h-recipe
  18. * h-product
  19. * h-item
  20. * h-feed
  21. ## Library
  22. XRay can be used as a library in your PHP project. The easiest way to install it and its dependencies is via composer.
  23. ```
  24. composer require p3k/xray
  25. ```
  26. You can also [download a release](https://github.com/aaronpk/XRay/releases) which is a zip file with all dependencies already installed.
  27. ### Parsing
  28. ```php
  29. $xray = new p3k\XRay();
  30. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/');
  31. ```
  32. If you already have an HTML or JSON document you want to parse, you can pass it as a string in the second parameter.
  33. ```php
  34. $xray = new p3k\XRay();
  35. $html = '<html>....</html>';
  36. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html);
  37. ```
  38. In both cases, you can add an additional parameter to configure various options of how XRay will behave. Below is a list of the options.
  39. * `timeout` - The timeout in seconds to wait for any HTTP requests
  40. * `max_redirects` - The maximum number of redirects to follow
  41. * `include_original` - Will also return the full document fetched
  42. * `target` - Specify a target URL, and XRay will first check if that URL is on the page, and only if it is, will continue to parse the page. This is useful when you're using XRay to verify an incoming webmention.
  43. * `expect=feed` - If you know the thing you are parsing is a feed, include this parameter which will avoid running the autodetection rules and will provide better results for some feeds.
  44. Additional parameters are supported when making requests that use the Twitter or GitHub API. See the Authentication section below for details.
  45. ```php
  46. $xray = new p3k\XRay();
  47. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', [
  48. 'timeout' => 30
  49. ]);
  50. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html, [
  51. 'target' => 'http://example.com/'
  52. ]);
  53. ```
  54. The `$parsed` return value will look like the below. See "Primary Data" below for an explanation of the vocabularies returned.
  55. ```
  56. $parsed = Array
  57. (
  58. [data] => Array
  59. (
  60. [type] => card
  61. [name] => Aaron Parecki
  62. [url] => https://aaronparecki.com/
  63. [photo] => https://aaronparecki.com/images/profile.jpg
  64. )
  65. [url] => https://aaronparecki.com/
  66. [code] => 200
  67. )
  68. ```
  69. ### Rels
  70. You can also use XRay to fetch all the rel values on a page, merging the list of HTTP `Link` headers with rel values with the HTML rel values on the page.
  71. ```php
  72. $xray = new p3k\XRay();
  73. $xray->http = $this->http;
  74. $rels = $xray->rels('https://aaronparecki.com/');
  75. ```
  76. This will return a similar response to the parser, but instead of a `data` key containing the parsed page, there will be `rels`, an associative array. Each key will contain an array of all the values that match that rel value.
  77. ```
  78. $rels = Array
  79. (
  80. [url] => https://aaronparecki.com/
  81. [code] => 200
  82. [rels] => Array
  83. (
  84. [hub] => Array
  85. (
  86. [0] => https://switchboard.p3k.io/
  87. )
  88. [authorization_endpoint] => Array
  89. (
  90. [0] => https://aaronparecki.com/auth
  91. )
  92. ...
  93. ```
  94. ### Customizing the User Agent
  95. To set a unique user agent, (some websites will require a user agent be set), you can set the `http` property of the object to a `p3k\HTTP` object.
  96. ```php
  97. $xray = new p3k\XRay();
  98. $xray->http = new p3k\HTTP('MyProject/1.0.0 (http://example.com/)');
  99. $xray->parse('http://example.com/');
  100. ```
  101. ## API
  102. XRay can also be used as an API to provide its parsing capabilities over an HTTP service.
  103. To parse a page and return structured data for the contents of the page, simply pass a url to the `/parse` route.
  104. ```
  105. GET /parse?url=https://aaronparecki.com/2016/01/16/11/
  106. ```
  107. To conditionally parse the page after first checking if it contains a link to a target URL, also include the target URL as a parameter. This is useful when using XRay to verify an incoming webmention.
  108. ```
  109. GET /parse?url=https://aaronparecki.com/2016/01/16/11/&target=http://example.com
  110. ```
  111. In both cases, the response will be a JSON object containing a key of "type". If there was an error, "type" will be set to the string "error", otherwise it will refer to the kind of content that was found at the URL, most often "entry".
  112. You can also make a POST request with the same parameter names.
  113. If you already have an HTML or JSON document you want to parse, you can include that in the POST parameter `body`. This POST request would look like the below:
  114. ```
  115. POST /parse
  116. Content-type: application/x-www-form-urlencoded
  117. url=https://aaronparecki.com/2016/01/16/11/
  118. &body=<html>....</html>
  119. ```
  120. or for Twitter/GitHub where you might have JSON,
  121. ```
  122. POST /parse
  123. Content-type: application/x-www-form-urlencoded
  124. url=https://github.com/aaronpk/XRay
  125. &body={"repo":......}
  126. ```
  127. ### Parameters
  128. XRay accepts the following parameters when calling `/parse`
  129. * `url` - the URL of the page to parse
  130. * `target` - Specify a target URL, and XRay will first check if that URL is on the page, and only if it is, will continue to parse the page. This is useful when you're using XRay to verify an incoming webmention.
  131. * `timeout` - The timeout in seconds to wait for any HTTP requests
  132. * `max_redirects` - The maximum number of redirects to follow
  133. * `include_original` - Will also return the full document fetched
  134. * `expect=feed` - If you know the thing you are parsing is a feed, include this parameter which will avoid running the autodetection rules and will provide better results for some feeds.
  135. ### Authentication
  136. If the URL you are fetching requires authentication, include the access token in the parameter "token", and it will be included in an "Authorization" header when fetching the URL. (It is recommended to use a POST request in this case, to avoid the access token potentially being logged as part of the query string.) This is useful for [Private Webmention](https://indieweb.org/Private-Webmention) verification.
  137. ```
  138. POST /parse
  139. url=https://aaronparecki.com/2016/01/16/11/
  140. &target=http://example.com
  141. &token=12341234123412341234
  142. ```
  143. ### Twitter Authentication
  144. XRay uses the Twitter API to fetch posts, and the Twitter API requires authentication. In order to keep XRay stateless, it is required that you pass in Twitter credentials to the parse call. You can register an application on the Twitter developer website, and generate an access token for your account without writing any code, and then use those credentials when making an API request to XRay.
  145. You should only send Twitter credentials when the URL you are trying to parse is a Twitter URL, so you'll want to check for whether the hostname is `twitter.com` before you include credentials in this call.
  146. * `twitter_api_key` - Your application's API key
  147. * `twitter_api_secret` - Your application's API secret
  148. * `twitter_access_token` - Your Twitter access token
  149. * `twitter_access_token_secret` - Your Twitter secret access token
  150. ### GitHub Authentication
  151. XRay uses the GitHub API to fetch GitHub URLs, which provides higher rate limits when used with authentication. You can pass a GitHub access token along with the request and XRay will use it when making requests to the API.
  152. * `github_access_token` - A GitHub access token
  153. ### Error Response
  154. ```json
  155. {
  156. "error": "not_found",
  157. "error_description": "The URL provided was not found"
  158. }
  159. ```
  160. Possible errors are listed below:
  161. * `not_found`: The URL provided was not found. (Returned 404 when fetching)
  162. * `ssl_cert_error`: There was an error validating the SSL certificate. This may happen if the SSL certificate has expired.
  163. * `ssl_unsupported_cipher`: The web server does not support any of the SSL ciphers known by the service.
  164. * `timeout`: The service timed out trying to connect to the URL.
  165. * `invalid_content`: The content at the URL was not valid. For example, providing a URL to an image will return this error.
  166. * `no_link_found`: The target link was not found on the page. When a target parameter is provided, this is the error that will be returned if the target could not be found on the page.
  167. * `no_content`: No usable content could be found at the given URL.
  168. * `unauthorized`: The URL returned HTTP 401 Unauthorized.
  169. * `forbidden`: The URL returned HTTP 403 Forbidden.
  170. ### Response Format
  171. ```json
  172. {
  173. "data":{
  174. "type":"entry",
  175. "published":"2017-03-01T19:00:33-08:00",
  176. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  177. "category":[
  178. "indieweb",
  179. "hwc"
  180. ],
  181. "photo":[
  182. "https://aaronparecki.com/2017/03/01/14/photo.jpg"
  183. ],
  184. "syndication":[
  185. "https://twitter.com/aaronpk/status/837135519427395584"
  186. ],
  187. "content":{
  188. "text":"Hello from Homebrew Website Club PDX! Thanks to @DreamHost for hosting us! 🍕🎉 #indieweb",
  189. "html":"Hello from Homebrew Website Club PDX! Thanks to <a href=\"https://twitter.com/DreamHost\">@DreamHost</a> for hosting us! <a href=\"https://aaronparecki.com/emoji/%F0%9F%8D%95\">🍕</a><a href=\"https://aaronparecki.com/emoji/%F0%9F%8E%89\">🎉</a> <a href=\"https://aaronparecki.com/tag/indieweb\">#indieweb</a>"
  190. },
  191. "author":{
  192. "type":"card",
  193. "name":"Aaron Parecki",
  194. "url":"https://aaronparecki.com/",
  195. "photo":"https://aaronparecki.com/images/profile.jpg"
  196. }
  197. },
  198. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  199. "code":200
  200. }
  201. ```
  202. #### Primary Data
  203. The primary object on the page is returned in the `data` property. This will indicate the type of object (e.g. `entry`), and will contain the vocabulary's properties that it was able to parse from the page.
  204. If a property supports multiple values, it will always be returned as an array. The following properties support multiple values:
  205. * `in-reply-to`
  206. * `like-of`
  207. * `repost-of`
  208. * `bookmark-of`
  209. * `syndication`
  210. * `photo` (of entry, not of a card)
  211. * `video`
  212. * `audio`
  213. * `category`
  214. The content will be an object that always contains a "text" property and may contain an "html" property if the source documented published HTML content. The "text" property must always be HTML escaped before displaying it as HTML, as it may include unescaped characters such as `<` and `>`.
  215. The author will always be set in the entry if available. The service follows the [authorship discovery](https://indieweb.org/authorship) algorithm to try to find the author information elsewhere on the page if it is not inside the entry in the source document.
  216. All URLs provided in the output are absolute URLs. If the source document contains a relative URL, it will be resolved first.
  217. #### Other Properties
  218. Other properties are returned in the response at the same level as the `data` property.
  219. * `url` - The effective URL that the document was retrieved from. This will be the final URL after following any redirects.
  220. * `code` - The HTTP response code returned by the URL. Typically this will be 200, but if the URL returned an alternate HTTP code that also included an h-entry (such as a 410 deleted notice with a stub h-entry), you can use this to find out that the original URL was actually deleted.
  221. #### Feeds
  222. XRay can return information for several kinds of feeds. The URL (or body) passed to XRay will be checked for the following formats:
  223. * XML (Atom and RSS)
  224. * JSONFeed (https://jsonfeed.org)
  225. * Microformats [h-feed](https://indieweb.org/h-feed)
  226. If the page being parsed represents a feed, then the response will look like the following:
  227. ```json
  228. {
  229. "data": {
  230. "type": "feed",
  231. "items": [
  232. ]
  233. }
  234. }
  235. ```
  236. Each object in the `items` array will contain a parsed version of the item, in the same format that XRay normally returns. When parsing Microformats feeds, the [authorship discovery](https://indieweb.org/authorship) will be run for each item to build out the author info.
  237. Atom, RSS and JSONFeed will all be normalized to XRay's vocabulary, and only recognized properties will be returned.
  238. ## Rels
  239. There is also an API method to parse and return all rel values on the page, including HTTP `Link` headers and HTML rel values.
  240. ```
  241. GET /rels?url=https://aaronparecki.com/
  242. ```
  243. ## Token API
  244. When verifying [Private Webmentions](https://indieweb.org/Private-Webmention#How_to_Receive_Private_Webmentions), you will need to exchange a code for an access token at the token endpoint specified by the source URL.
  245. XRay provides an API that will do this in one step. You can provide the source URL and code you got from the webmention, and XRay will discover the token endpoint, and then return you an access token.
  246. ```
  247. POST /token
  248. source=http://example.com/private-post
  249. &code=1234567812345678
  250. ```
  251. The response will be the response from the token endpoint, which will include an `access_token` property, and possibly an `expires_in` property.
  252. ```
  253. {
  254. "access_token": "eyJ0eXAXBlIjoI6Imh0dHB8idGFyZ2V0IjoraW0uZGV2bb-ZO6MV-DIqbUn_3LZs",
  255. "token_type": "bearer",
  256. "expires_in": 3600
  257. }
  258. ```
  259. If there was a problem fetching the access token, you will get one of the errors below in addition to the HTTP related errors returned by the parse API:
  260. * `no_token_endpoint` - Unable to find an HTTP header specifying the token endpoint.
  261. ## Installation
  262. ### From Source
  263. ```
  264. # Clone this repository
  265. git clone git@github.com:aaronpk/XRay.git
  266. cd XRay
  267. # Install dependencies
  268. composer install
  269. ```
  270. ### From Zip Archive
  271. * Download the latest release from https://github.com/aaronpk/XRay/releases
  272. * Extract to a folder on your web server
  273. ### Web Server Configuration
  274. Configure your web server to point to the `public` folder.
  275. Make sure all requests are routed to `index.php`. XRay ships with `.htaccess` files for Apache. For nginx, you'll need a rule like the following in your server config block.
  276. ```
  277. try_files $uri /index.php?$args;
  278. ```