Skip to content

Commit 3e4254a

Browse files
author
Andrei Igna
committed
Add - Readme & examples
1 parent d02bbf5 commit 3e4254a

4 files changed

Lines changed: 105 additions & 3 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "layered/url-preview",
3-
"description": "Get data from any URL from the internet",
3+
"description": "Get preview data from any URL from the internet",
44
"type": "library",
5+
"keywords": ["scraper", "url-preview", "opengraph"],
56
"require": {
67
"fabpot/goutte": "^3.2"
78
},

example.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
6+
echo '<pre>';
7+
8+
echo '<h1>YouTube</h1>';
9+
print_r(Layered\UrlPreview::load('https://www.youtube.com/')->getData());
10+
print_r(Layered\UrlPreview::load('https://www.youtube.com/watch?v=3iTYpWfP0Og')->getData());
11+
12+
echo '<h1>WordPress</h1>';
13+
print_r(Layered\UrlPreview::load('http://roberthajnal.ro/blog/2017/07/23/maraton-7500-2017/')->getData());
14+
print_r(Layered\UrlPreview::load('http://roberthajnal.ro/blog/2017/07/01/lavaredo-ultra-trail-la-ultra-si-cei-mai-buni-din-lume-se-taie/')->getData());
15+
16+
echo '<h1>Medium</h1>';
17+
print_r(Layered\UrlPreview::load('https://rokm.ro/')->getData());
18+
print_r(Layered\UrlPreview::load('https://medium.com/@andrewcouldwell/plasma-design-system-4d63fb6c1afc')->getData());
19+
print_r(Layered\UrlPreview::load('https://rokm.ro/what-makes-a-good-fitness-app-b5416d410002')->getData());
20+
21+
echo '<h1>Instagram</h1>';
22+
print_r(Layered\UrlPreview::load('https://instagram.com')->getData());
23+
print_r(Layered\UrlPreview::load('https://instagram.com/andreihere')->getData());
24+
print_r(Layered\UrlPreview::load('https://instagr.am/p/BYOgma8hoUK/')->getData());
25+
print_r(Layered\UrlPreview::load('https://www.instagram.com/p/BYjGxqkgQCm/?taken-by=daria_solak_illustrations')->getData());
26+
print_r(Layered\UrlPreview::load('https://www.instagram.com/p/BPezVTTjP7g/?taken-by=andreihere')->getData());
27+
28+
echo '<h1>Facebook</h1>';
29+
print_r(Layered\UrlPreview::load('https://facebook.com')->getData());
30+
print_r(Layered\UrlPreview::load('https://facebook.com/andreiigna')->getData());
31+
print_r(Layered\UrlPreview::load('https://www.facebook.com/cciuca/posts/1857443374282334')->getData());
32+
print_r(Layered\UrlPreview::load('https://www.facebook.com/mxgur/posts/10107274070905477')->getData());
33+
print_r(Layered\UrlPreview::load('https://www.facebook.com/photo.php?fbid=1889887641037956&set=a.764654416894623.1073741851.100000502141017&type=3&theater')->getData());
34+
35+
echo '<h1>Twitter</h1>';
36+
print_r(Layered\UrlPreview::load('https://twitter.com')->getData());
37+
print_r(Layered\UrlPreview::load('https://twitter.com/AndreiIgna')->getData());
38+
print_r(Layered\UrlPreview::load('https://twitter.com/AndreiIgna/status/903558303065759744')->getData());
39+
print_r(Layered\UrlPreview::load('https://twitter.com/AndreiIgna/status/890510906819571719')->getData());
40+
print_r(Layered\UrlPreview::load('https://twitter.com/AndreiIgna/status/902280395336626176')->getData());
41+
42+
echo '</pre>';

readme.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
URL Preview
2+
=========
3+
4+
**URL Preview** is a PHP library than can get preview info on any URL from the internet! It uses data from [OpenGraph](http://ogp.me/) tags with fallback to HTML meta tags.
5+
6+
## Use cases
7+
8+
* Display Info Cards for links in a article
9+
* Rich preview for links in messaging apps
10+
* Enable file/image upload from URLs
11+
* *many more*
12+
13+
## How to use
14+
15+
#### Installation
16+
17+
Add `layered/url-preview` as a require dependency in your `composer.json` file:
18+
19+
#### Usage
20+
21+
For each URL, create a `UrlPreview` instance with URL as first argument. Data is retrieved with `getData()` method:
22+
```
23+
$url = new Layered\UrlPreview('https://instagr.am/p/BYOgma8hoUK/');
24+
$urlData = $url->getData();
25+
26+
// shorthand method
27+
$urlData = Layered\UrlPreview::load('https://instagr.am/p/BYOgma8hoUK/')->getData();
28+
```
29+
30+
#### Preview data
31+
32+
The returned data from URLs is an Array with the following structure:
33+
```
34+
[
35+
'type' => 'photo', // content type: website, article, photo, etc
36+
'url' => 'https://www.instagram.com/p/BYOgma8hoUK/', // processed URL: follows redirects, formatted by site
37+
'title' => 'Instagram post by Andrei • Aug 25, 2017 at 6:23pm UTC',
38+
'image' => 'https://scontent-mad1-1.cdninstagram.com/t51.2885-15/e35/21149037_113894809286463_3471808960858685440_n.jpg',
39+
'description' => 'Who needs a gym membership when there’s this? Outdoor gym, on the beach #barcelona #beach #gym…'
40+
]
41+
```
42+
Each URL may return extra fields, present in OpenGraph tags or added by a `Layered\UrlPreview\Formatter`. Example of `profile` field for Twitter & Instagram URLS:
43+
44+
```
45+
[
46+
// always present tags
47+
'profile' => [
48+
'name' => 'Andrei',
49+
'username' => '@andreihere',
50+
'url' => 'https://www.instagram.com/andreihere/'
51+
]
52+
]
53+
```
54+
55+
56+
## More
57+
58+
Please report any issues here on GitHub.
59+
Any contributions are welcome (please get in touch before submitting a PR).

src/UrlPreview.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($url) {
3434
if (!$this->previewer && $previewer->test($crawler)) {
3535
$this->previewer = $previewer;
3636
$this->data = $previewer->getData($crawler);
37-
$this->data['previewer'] = $previewer->getName();
37+
//$this->data['previewer'] = $previewer->getName();
3838
}
3939
}
4040
}
@@ -77,6 +77,6 @@ public static function addFormatter($formatter, $priority = 10) {
7777
UrlPreview::addPreviewer(new Previewer\OpenGraph);
7878
UrlPreview::addPreviewer(new Previewer\SimpleHtml, 100);
7979

80-
// Formatters to get extra Twitter & Instagram profile info
80+
// Formatters to get extra Twitter & Instagram profile info
8181
UrlPreview::addFormatter(new Formatter\InstagramFromOpenGraph);
8282
UrlPreview::addFormatter(new Formatter\TwitterFields);

0 commit comments

Comments
 (0)