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.

393 lines
13 KiB

8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
  1. XRay
  2. ====
  3. XRay parses structured content from a URL.
  4. ## Discovering Content
  5. The contents of the URL is checked in the following order:
  6. * A silo URL from one of the following websites:
  7. * Instagram
  8. * Twitter
  9. * GitHub
  10. * XKCD
  11. * (more coming soon)
  12. * Microformats
  13. * h-card
  14. * h-entry
  15. * h-event
  16. * h-review
  17. * h-recipe
  18. * h-product
  19. * h-item
  20. ## Library
  21. XRay can be used as a library in your PHP project. The easiest way to install it and its dependencies is via composer.
  22. ```
  23. composer require p3k/xray
  24. ```
  25. ### Parsing
  26. ```php
  27. $xray = new p3k\XRay();
  28. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/');
  29. ```
  30. If you already have an HTML or JSON document you want to parse, you can pass it as a string in the second parameter.
  31. ```php
  32. $xray = new p3k\XRay();
  33. $html = '<html>....</html>';
  34. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html);
  35. ```
  36. 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.
  37. * `timeout` - The timeout in seconds to wait for any HTTP requests
  38. * `max_redirects` - The maximum number of redirects to follow
  39. * `include_original` - Will also return the full document fetched
  40. * `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.
  41. Additionally, the following parameters are supported when making requests that use the Twitter or GitHub API. See the authentication section below for details.
  42. ```php
  43. $xray = new p3k\XRay();
  44. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', [
  45. 'timeout' => 30
  46. ]);
  47. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html, [
  48. 'target' => 'http://example.com/'
  49. ]);
  50. ```
  51. The `$parsed` return value will look like the below. See "Primary Data" below for an explanation of the vocabularies returned.
  52. ```
  53. $parsed = Array
  54. (
  55. [data] => Array
  56. (
  57. [type] => card
  58. [name] => Aaron Parecki
  59. [url] => https://aaronparecki.com/
  60. [photo] => https://aaronparecki.com/images/profile.jpg
  61. )
  62. [url] => https://aaronparecki.com/
  63. [code] => 200
  64. )
  65. ```
  66. ### Rels
  67. 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.
  68. ```php
  69. $xray = new p3k\XRay();
  70. $xray->http = $this->http;
  71. $rels = $xray->rels('https://aaronparecki.com/');
  72. ```
  73. 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.
  74. ```
  75. $rels = Array
  76. (
  77. [url] => https://aaronparecki.com/
  78. [code] => 200
  79. [rels] => Array
  80. (
  81. [hub] => Array
  82. (
  83. [0] => https://switchboard.p3k.io/
  84. )
  85. [authorization_endpoint] => Array
  86. (
  87. [0] => https://aaronparecki.com/auth
  88. )
  89. ...
  90. ```
  91. ### Customizing the User Agent
  92. 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.
  93. ```php
  94. $xray = new p3k\XRay();
  95. $xray->http = new p3k\HTTP('MyProject/1.0.0 (http://example.com/)');
  96. $xray->parse('http://example.com/');
  97. ```
  98. ## API
  99. XRay can also be used as an API to provide its parsing capabilities over an HTTP service.
  100. To parse a page and return structured data for the contents of the page, simply pass a url to the parse route.
  101. ```
  102. GET /parse?url=https://aaronparecki.com/2016/01/16/11/
  103. ```
  104. 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.
  105. ```
  106. GET /parse?url=https://aaronparecki.com/2016/01/16/11/&target=http://example.com
  107. ```
  108. 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".
  109. You can also make a POST request with the same parameter names.
  110. If you already have an HTML or JSON document you want to parse, you can include that in the parameter `body`. This POST request would look like the below:
  111. ```
  112. POST /parse
  113. Content-type: application/x-www-form-urlencoded
  114. url=https://aaronparecki.com/2016/01/16/11/
  115. &body=<html>....</html>
  116. ```
  117. or for Twitter/GitHub where you might have JSON,
  118. ```
  119. POST /parse
  120. Content-type: application/x-www-form-urlencoded
  121. url=https://github.com/aaronpk/XRay
  122. &body={"repo":......}
  123. ```
  124. ### Authentication
  125. 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.
  126. ```
  127. POST /parse
  128. url=https://aaronparecki.com/2016/01/16/11/
  129. &target=http://example.com
  130. &token=12341234123412341234
  131. ```
  132. ### Twitter Authentication
  133. 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.
  134. 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.
  135. * twitter_api_key - Your application's API key
  136. * twitter_api_secret - Your application's API secret
  137. * twitter_access_token - Your Twitter access token
  138. * twitter_access_token_secret - Your Twitter secret access token
  139. ### GitHub Authentication
  140. 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.
  141. * github_access_token - A GitHub access token
  142. ### Error Response
  143. ```json
  144. {
  145. "error": "not_found",
  146. "error_description": "The URL provided was not found"
  147. }
  148. ```
  149. Possible errors are listed below:
  150. * `not_found`: The URL provided was not found. (Returned 404 when fetching)
  151. * `ssl_cert_error`: There was an error validating the SSL certificate. This may happen if the SSL certificate has expired.
  152. * `ssl_unsupported_cipher`: The web server does not support any of the SSL ciphers known by the service.
  153. * `timeout`: The service timed out trying to connect to the URL.
  154. * `invalid_content`: The content at the URL was not valid. For example, providing a URL to an image will return this error.
  155. * `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.
  156. * `no_content`: No usable content could be found at the given URL.
  157. * `unauthorized`: The URL returned HTTP 401 Unauthorized.
  158. * `forbidden`: The URL returned HTTP 403 Forbidden.
  159. ### Response Format
  160. ```json
  161. {
  162. "data":{
  163. "type":"entry",
  164. "published":"2017-03-01T19:00:33-08:00",
  165. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  166. "category":[
  167. "indieweb",
  168. "hwc"
  169. ],
  170. "photo":[
  171. "https://aaronparecki.com/2017/03/01/14/photo.jpg"
  172. ],
  173. "syndication":[
  174. "https://twitter.com/aaronpk/status/837135519427395584"
  175. ],
  176. "content":{
  177. "text":"Hello from Homebrew Website Club PDX! Thanks to @DreamHost for hosting us! 🍕🎉 #indieweb",
  178. "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>"
  179. },
  180. "author":{
  181. "type":"card",
  182. "name":"Aaron Parecki",
  183. "url":"https://aaronparecki.com/",
  184. "photo":"https://aaronparecki.com/images/profile.jpg"
  185. }
  186. },
  187. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  188. "code":200
  189. }
  190. ```
  191. #### Primary Data
  192. 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.
  193. If a property supports multiple values, it will always be returned as an array. The following properties support multiple values:
  194. * in-reply-to
  195. * like-of
  196. * repost-of
  197. * bookmark-of
  198. * syndication
  199. * photo (of entry, not of a card)
  200. * video
  201. * audio
  202. * category
  203. 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 `>`.
  204. The author will always be set in the entry if available. The service follows the [authorship discovery](http://indiewebcamp.com/authorship) algorithm to try to find the author information elsewhere on the page if it is not inside the entry in the source document.
  205. All URLs provided in the output are absolute URLs. If the source document contains a relative URL, it will be resolved first.
  206. In a future version, replies, likes, reposts, etc. of this post will be included if they are listed on the page.
  207. ```json
  208. {
  209. "data": {
  210. "type": "entry",
  211. ...
  212. "like": [
  213. {
  214. "type": "cite",
  215. "author": {
  216. "type": "card",
  217. "name": "Thomas Dunlap",
  218. "photo": "https://s3-us-west-2.amazonaws.com/aaronparecki.com/twitter.com/9055c458a67762637c0071006b16c78f25cb610b224dbc98f48961d772faff4d.jpeg",
  219. "url": "https://twitter.com/spladow"
  220. },
  221. "url": "https://twitter.com/aaronpk/status/688518372170977280#favorited-by-16467582"
  222. }
  223. ],
  224. "comment": [
  225. {
  226. "type": "cite",
  227. "author": {
  228. "type": "card",
  229. "name": "Poetica",
  230. "photo": "https://s3-us-west-2.amazonaws.com/aaronparecki.com/twitter.com/192664bb706b2998ed42a50a860490b6aa1bb4926b458ba293b4578af599aa6f.png",
  231. "url": "http://poetica.com/"
  232. },
  233. "url": "https://twitter.com/poetica/status/689045331426803712",
  234. "published": "2016-01-18T03:23:03-08:00",
  235. "content": {
  236. "text": "@aaronpk @mozillapersona thanks very much! :)"
  237. }
  238. }
  239. ]
  240. }
  241. }
  242. ```
  243. #### Other Properties
  244. Other properties are returned in the response at the same level as the `data` property.
  245. * `url` - The effective URL that the document was retrieved from. This will be the final URL after following any redirects.
  246. * `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.
  247. ## Rels
  248. There is also an API method to parse and return all rel values on the page, including HTTP `Link` headers and HTML rel values.
  249. ```
  250. GET /rels?url=https://aaronparecki.com/
  251. ```
  252. ## Token API
  253. 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.
  254. 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.
  255. ```
  256. POST /token
  257. source=http://example.com/private-post
  258. &code=1234567812345678
  259. ```
  260. The response will be the response from the token endpoint, which will include an `access_token` property, and possibly an `expires_in` property.
  261. ```
  262. {
  263. "access_token": "eyJ0eXAXBlIjoI6Imh0dHB8idGFyZ2V0IjoraW0uZGV2bb-ZO6MV-DIqbUn_3LZs",
  264. "token_type": "bearer",
  265. "expires_in": 3600
  266. }
  267. ```
  268. 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:
  269. * `no_token_endpoint` - Unable to find an HTTP header specifying the token endpoint.
  270. ## Installation
  271. ### From Source
  272. ```
  273. # Clone this repository
  274. git clone git@github.com:aaronpk/XRay.git
  275. cd XRay
  276. # Install dependencies
  277. composer install
  278. ```
  279. ### From Zip Archive
  280. * Download the latest release from https://github.com/aaronpk/XRay/releases
  281. * Extract to a folder on your web server
  282. ### Web Server Configuration
  283. Configure your web server to point to the `public` folder.
  284. 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.
  285. ```
  286. try_files $uri /index.php?$args;
  287. ```