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.

500 lines
16 KiB

8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. * Facebook (public events)
  12. If the contents of the URL is XML or JSON, then XRay will parse the Atom, RSS or JSONFeed formats.
  13. Finally, XRay looks for Microformats on the page and will determine the content from that.
  14. * h-card
  15. * h-entry
  16. * h-event
  17. * h-review
  18. * h-recipe
  19. * h-product
  20. * h-item
  21. * h-feed
  22. ## Library
  23. XRay can be used as a library in your PHP project. The easiest way to install it and its dependencies is via composer.
  24. ```
  25. composer require p3k/xray
  26. ```
  27. You can also [download a release](https://github.com/aaronpk/XRay/releases) which is a zip file with all dependencies already installed.
  28. ### Parsing
  29. ```php
  30. $xray = new p3k\XRay();
  31. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/');
  32. ```
  33. If you already have an HTML or JSON document you want to parse, you can pass it as a string in the second parameter.
  34. ```php
  35. $xray = new p3k\XRay();
  36. $html = '<html>....</html>';
  37. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html);
  38. ```
  39. ```php
  40. $xray = new p3k\XRay();
  41. $jsonfeed = '{"version":"https://jsonfeed.org/version/1","title":"Manton Reece", ... ';
  42. // Note that the JSON document must be passed in as a string in this case
  43. $parsed = $xray->parse('https://manton.micro.blog/feed.json', $jsonfeed);
  44. ```
  45. 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.
  46. * `timeout` - The timeout in seconds to wait for any HTTP requests
  47. * `max_redirects` - The maximum number of redirects to follow
  48. * `include_original` - Will also return the full document fetched
  49. * `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.
  50. * `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.
  51. Additional parameters are supported when making requests that use the Twitter or GitHub API. See the Authentication section below for details.
  52. ```php
  53. $xray = new p3k\XRay();
  54. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', [
  55. 'timeout' => 30
  56. ]);
  57. $parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html, [
  58. 'target' => 'http://example.com/'
  59. ]);
  60. ```
  61. The `$parsed` return value will look like the below. See "Primary Data" below for an explanation of the vocabularies returned.
  62. ```
  63. $parsed = Array
  64. (
  65. [data] => Array
  66. (
  67. [type] => card
  68. [name] => Aaron Parecki
  69. [url] => https://aaronparecki.com/
  70. [photo] => https://aaronparecki.com/images/profile.jpg
  71. )
  72. [url] => https://aaronparecki.com/
  73. [code] => 200
  74. )
  75. ```
  76. ### Processing Microformats2 JSON
  77. If you already have a parsed Microformats2 document as an array, you can use a special function to process it into XRay's native format. Make sure you pass the entire parsed document, not just the single item.
  78. ```php
  79. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  80. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  81. $xray = new p3k\XRay();
  82. $parsed = $xray->process('http://example.com/entry', $mf2); // note the use of `process` not `parse`
  83. Array
  84. (
  85. [data] => Array
  86. (
  87. [type] => entry
  88. [photo] => Array
  89. (
  90. [0] => http://example.com/photo.jpg
  91. )
  92. [content] => Array
  93. (
  94. [text] => Hello World
  95. )
  96. )
  97. [url] => http://example.com/entry
  98. )
  99. ```
  100. ### Rels
  101. 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.
  102. ```php
  103. $xray = new p3k\XRay();
  104. $rels = $xray->rels('https://aaronparecki.com/');
  105. ```
  106. 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.
  107. ```
  108. Array
  109. (
  110. [url] => https://aaronparecki.com/
  111. [code] => 200
  112. [rels] => Array
  113. (
  114. [hub] => Array
  115. (
  116. [0] => https://switchboard.p3k.io/
  117. )
  118. [authorization_endpoint] => Array
  119. (
  120. [0] => https://aaronparecki.com/auth
  121. )
  122. ...
  123. ```
  124. ### Feed Discovery
  125. You can use XRay to discover the types of feeds available at a URL.
  126. ```php
  127. $xray = new p3k\XRay();
  128. $feeds = $xray->feeds('http://percolator.today');
  129. ```
  130. This will fetch the URL, check for a Microformats feed, as well as check for rel=alternates pointing to Atom, RSS or JSONFeed URLs. The response will look like the below.
  131. ```
  132. Array
  133. (
  134. [url] => https://percolator.today/
  135. [code] => 200
  136. [feeds] => Array
  137. (
  138. [0] => Array
  139. (
  140. [url] => https://percolator.today/
  141. [type] => microformats
  142. )
  143. [1] => Array
  144. (
  145. [url] => https://percolator.today/podcast.xml
  146. [type] => rss
  147. )
  148. )
  149. )
  150. ```
  151. ### Customizing the User Agent
  152. 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.
  153. ```php
  154. $xray = new p3k\XRay();
  155. $xray->http = new p3k\HTTP('MyProject/1.0.0 (http://example.com/)');
  156. $xray->parse('http://example.com/');
  157. ```
  158. ## API
  159. XRay can also be used as an API to provide its parsing capabilities over an HTTP service.
  160. To parse a page and return structured data for the contents of the page, simply pass a url to the `/parse` route.
  161. ```
  162. GET /parse?url=https://aaronparecki.com/2016/01/16/11/
  163. ```
  164. 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.
  165. ```
  166. GET /parse?url=https://aaronparecki.com/2016/01/16/11/&target=http://example.com
  167. ```
  168. 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".
  169. You can also make a POST request with the same parameter names.
  170. 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:
  171. ```
  172. POST /parse
  173. Content-type: application/x-www-form-urlencoded
  174. url=https://aaronparecki.com/2016/01/16/11/
  175. &body=<html>....</html>
  176. ```
  177. or for Twitter/GitHub/Facebook where you might have JSON,
  178. ```
  179. POST /parse
  180. Content-type: application/x-www-form-urlencoded
  181. url=https://github.com/aaronpk/XRay
  182. &body={"repo":......}
  183. ```
  184. ### Parameters
  185. XRay accepts the following parameters when calling `/parse`
  186. * `url` - the URL of the page to parse
  187. * `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.
  188. * `timeout` - The timeout in seconds to wait for any HTTP requests
  189. * `max_redirects` - The maximum number of redirects to follow
  190. * `include_original` - Will also return the full document fetched
  191. * `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.
  192. ### Authentication
  193. 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.
  194. ```
  195. POST /parse
  196. url=https://aaronparecki.com/2016/01/16/11/
  197. &target=http://example.com
  198. &token=12341234123412341234
  199. ```
  200. ### API Authentication
  201. XRay uses the Twitter, Github and Facebook APIs to fetch posts, and those API require authentication. In order to keep XRay stateless, it is required that you pass in the credentials to the parse call.
  202. You should only send the credentials when the URL you are trying to parse is a Twitter URL, a GitHub URL or a Facebook URL, so you'll want to check for whether the hostname is `twitter.com`, `github.com`, etc. before you include credentials in this call.
  203. #### Twitter Authentication
  204. XRay uses the Twitter API to fetch Twitter URLs. 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.
  205. * `twitter_api_key` - Your application's API key
  206. * `twitter_api_secret` - Your application's API secret
  207. * `twitter_access_token` - Your Twitter access token
  208. * `twitter_access_token_secret` - Your Twitter secret access token
  209. #### GitHub Authentication
  210. 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.
  211. * `github_access_token` - A GitHub access token
  212. #### Facebook Authentication
  213. XRay uses the Facebook API to fetch Facebook URLs. You can create a Facebook App on Facebooks developer website.
  214. * facebook_app_id - Your application's App ID
  215. * facebook_app_secret - Your application's App Secret
  216. At this moment, XRay is able to get it's own access token from those credentials.
  217. ### Error Response
  218. ```json
  219. {
  220. "error": "not_found",
  221. "error_description": "The URL provided was not found"
  222. }
  223. ```
  224. Possible errors are listed below:
  225. * `not_found`: The URL provided was not found. (Returned 404 when fetching)
  226. * `ssl_cert_error`: There was an error validating the SSL certificate. This may happen if the SSL certificate has expired.
  227. * `ssl_unsupported_cipher`: The web server does not support any of the SSL ciphers known by the service.
  228. * `timeout`: The service timed out trying to connect to the URL.
  229. * `invalid_content`: The content at the URL was not valid. For example, providing a URL to an image will return this error.
  230. * `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.
  231. * `no_content`: No usable content could be found at the given URL.
  232. * `unauthorized`: The URL returned HTTP 401 Unauthorized.
  233. * `forbidden`: The URL returned HTTP 403 Forbidden.
  234. ### Response Format
  235. ```json
  236. {
  237. "data":{
  238. "type":"entry",
  239. "published":"2017-03-01T19:00:33-08:00",
  240. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  241. "category":[
  242. "indieweb",
  243. "hwc"
  244. ],
  245. "photo":[
  246. "https://aaronparecki.com/2017/03/01/14/photo.jpg"
  247. ],
  248. "syndication":[
  249. "https://twitter.com/aaronpk/status/837135519427395584"
  250. ],
  251. "content":{
  252. "text":"Hello from Homebrew Website Club PDX! Thanks to @DreamHost for hosting us! 🍕🎉 #indieweb",
  253. "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>"
  254. },
  255. "author":{
  256. "type":"card",
  257. "name":"Aaron Parecki",
  258. "url":"https://aaronparecki.com/",
  259. "photo":"https://aaronparecki.com/images/profile.jpg"
  260. }
  261. },
  262. "url":"https://aaronparecki.com/2017/03/01/14/hwc",
  263. "code":200
  264. }
  265. ```
  266. #### Primary Data
  267. 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.
  268. If a property supports multiple values, it will always be returned as an array. The following properties support multiple values:
  269. * `in-reply-to`
  270. * `like-of`
  271. * `repost-of`
  272. * `bookmark-of`
  273. * `syndication`
  274. * `photo` (of entry, not of a card)
  275. * `video`
  276. * `audio`
  277. * `category`
  278. 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 `>`.
  279. 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.
  280. All URLs provided in the output are absolute URLs. If the source document contains a relative URL, it will be resolved first.
  281. #### Other Properties
  282. Other properties are returned in the response at the same level as the `data` property.
  283. * `url` - The effective URL that the document was retrieved from. This will be the final URL after following any redirects.
  284. * `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.
  285. #### Feeds
  286. XRay can return information for several kinds of feeds. The URL (or body) passed to XRay will be checked for the following formats:
  287. * XML (Atom and RSS)
  288. * JSONFeed (https://jsonfeed.org)
  289. * Microformats [h-feed](https://indieweb.org/h-feed)
  290. If the page being parsed represents a feed, then the response will look like the following:
  291. ```json
  292. {
  293. "data": {
  294. "type": "feed",
  295. "items": [
  296. {...},
  297. {...}
  298. ]
  299. }
  300. }
  301. ```
  302. 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.
  303. Atom, RSS and JSONFeed will all be normalized to XRay's vocabulary, and only recognized properties will be returned.
  304. ## Rels API
  305. There is also an API method to parse and return all rel values on the page, including HTTP `Link` headers and HTML rel values.
  306. ```
  307. GET /rels?url=https://aaronparecki.com/
  308. ```
  309. See [above](#rels) for the response format.
  310. ## Feed Discovery API
  311. ```
  312. GET /feeds?url=https://aaronparecki.com/
  313. ```
  314. See [above](#feed-discovery) for the response format.
  315. ## Token API
  316. 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.
  317. 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.
  318. ```
  319. POST /token
  320. source=http://example.com/private-post
  321. &code=1234567812345678
  322. ```
  323. The response will be the response from the token endpoint, which will include an `access_token` property, and possibly an `expires_in` property.
  324. ```
  325. {
  326. "access_token": "eyJ0eXAXBlIjoI6Imh0dHB8idGFyZ2V0IjoraW0uZGV2bb-ZO6MV-DIqbUn_3LZs",
  327. "token_type": "bearer",
  328. "expires_in": 3600
  329. }
  330. ```
  331. 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:
  332. * `no_token_endpoint` - Unable to find an HTTP header specifying the token endpoint.
  333. ## Installation
  334. ### From Source
  335. ```
  336. # Clone this repository
  337. git clone git@github.com:aaronpk/XRay.git
  338. cd XRay
  339. # Install dependencies
  340. composer install
  341. ```
  342. ### From Zip Archive
  343. * Download the latest release from https://github.com/aaronpk/XRay/releases
  344. * Extract to a folder on your web server
  345. ### Web Server Configuration
  346. Configure your web server to point to the `public` folder.
  347. 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.
  348. ```
  349. try_files $uri /index.php?$args;
  350. ```