Skip to content

Commit 6850ad4

Browse files
committed
improved readme
1 parent eb72c89 commit 6850ad4

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ Full library refactoring.
1414
- `cms` value
1515
- `language` to detect the page language
1616
- `languages` to detect urls to versions in different languages
17+
- `favicon` to detect small favicons (16 or 32px)
18+
- `icon` to detect big icons (from 48px)
1719

1820
### Changed
19-
- Changed the providers approach by independent detectors.
21+
- Changed providers (oEmbed, Html, OpenGraph etc) by independent detectors (title, url, language etc).
2022
- The `tags` value is renamed to `keywords`
2123
- Use Psr standards instead custom interfaces.
22-
- `providerImage` is renamed to `favicon` (small icons) and `icon` (big icons)
2324

2425
### Removed
26+
- Support for PHP<7.4
2527
- `type` value (is was very confusing)
2628
- `images` value
29+
- `providerImage` (use `favicon` or `icon` instead)
2730
- Support for files (pdf, jpg, video, etc).

README.md

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Requirements:
2020
>
2121
* If you need PHP 5.3 support, use the 1.x version
2222
* If you need PHP 5.4 support, use the 2.x version
23-
* If you need PHP 5.5 support, use the 3.x version
23+
* If you need PHP 5.5-7.3 support, use the 3.x version
2424

2525
## Online demo
2626

27-
http://oscarotero.com/embed3/demo
27+
http://oscarotero.com/embed/demo
2828

2929
## Installation
3030

@@ -96,13 +96,12 @@ foreach ($infos as $info) {
9696

9797
## Document
9898

99-
The document is the object that store the html code of the page. Like with oEmbed, you can use it to extract extra info:
99+
The document is the object that store the html code of the page. You can use it to extract extra info from the html code:
100100

101101
```php
102102
//Get the document object
103103
$document = $info->getDocument();
104104

105-
$document->meta('twitter:image'); //Returns the content of a <meta>
106105
$document->link('image_src'); //Returns the href of a <link>
107106
$document->getDocument(); //Returns the DOMDocument instance
108107
$html = (string) $document; //Returns the html code
@@ -119,25 +118,41 @@ $result = $document->select('.//a');
119118
//Filter the results
120119
$result->filter(fn ($node) => $node->getAttribute('href'));
121120

122-
$result->str('id'); //Return the id of the first result as string
123-
$result->str(); //Return the content of the first result
121+
$id = $result->str('id'); //Return the id of the first result as string
122+
$text = $result->str(); //Return the content of the first result
124123

125-
$result->strAll('id'); //Return an array with the ids of all results as string
126-
$result->strAll(); //Return an array with the content of all results as string
124+
$ids = $result->strAll('id'); //Return an array with the ids of all results as string
125+
$texts = $result->strAll(); //Return an array with the content of all results as string
127126

128-
$result->int('tabindex'); //Return the tabindex attribute of the first result as integer
129-
$result->int(); //Return the content of the first result as integer
127+
$tabindex = $result->int('tabindex'); //Return the tabindex attribute of the first result as integer
128+
$number = $result->int(); //Return the content of the first result as integer
130129

131-
$result->url('href'); //Return the href attribute of the first result as url (converts relative urls to absolutes)
132-
$result->url(); //Return the content of the first result as url
130+
$href = $result->url('href'); //Return the href attribute of the first result as url (converts relative urls to absolutes)
131+
$url = $result->url(); //Return the content of the first result as url
133132

134-
$result->node(); //Return the first node found (DOMElement)
135-
$result->nodes(); //Return all nodes found
133+
$node = $result->node(); //Return the first node found (DOMElement)
134+
$nodes = $result->nodes(); //Return all nodes found
135+
```
136+
137+
## Metas
138+
139+
For convenience, the object `Metas` stores the value of all `<meta>` elements located in the html, so you can get the values easier. The key of every meta is get from the `name`, `property` or `itemprop` attributes and the value is get from `content`.
140+
141+
```php
142+
//Get the Metas object
143+
$metas = $info->getMetas();
144+
145+
$metas->all(); //Return all values
146+
$metas->get('og:title'); //Return a key value
147+
$metas->str('og:title'); //Return the value as string (remove html tags)
148+
$metas->html('og:description'); //Return the value as html
149+
$metas->int('og:video:width'); //Return the value as integer
150+
$metas->url('og:url'); //Return the value as full url (converts relative urls to absolutes)
136151
```
137152

138153
## OEmbed
139154

140-
In addition to the html, this library uses [oEmbed](https://oembed.com/) endpoints to get additional data. You can get this data as following:
155+
In addition to the html and metas, this library uses [oEmbed](https://oembed.com/) endpoints to get additional data. You can get this data as following:
141156

142157
```php
143158
//Get the oEmbed object
@@ -153,7 +168,7 @@ $oembed->url('url'); //Return the value as full url (converts relative urls to a
153168

154169
## LinkedData
155170

156-
Other API used to extract info is [JsonLD](https://www.w3.org/TR/json-ld/).
171+
Another API available by default, used to extract info using the [JsonLD](https://www.w3.org/TR/json-ld/) schema.
157172

158173
```php
159174
//Get the linkedData object
@@ -263,10 +278,10 @@ class Robots extends Detector
263278
public function detect(): ?string
264279
{
265280
$response = $this->extractor->getResponse();
266-
$document = $this->extractor->getDocument();
281+
$metas = $this->extractor->getMetas();
267282

268283
return $response->getHeaderLine('x-robots-tag'),
269-
?: $document->meta('robots');
284+
?: $metas->str('robots');
270285
}
271286
}
272287

@@ -278,6 +293,23 @@ $embed->getExtractorFactory()->addDetector('robots', Robots::class);
278293
$info = $embed->get('http://example.com');
279294
$robots = $info->robots;
280295
```
296+
297+
### Settings
298+
299+
If you need to pass settings to your detectors, you can use the `setSettings` method:
300+
301+
```php
302+
//Create the extractor
303+
$info = $embed->get($url);
304+
305+
//Pass settings for example.com site
306+
if ($info->getUri()->getHost() === 'example.com') {
307+
$info->setSettings(['example_api_key' => 'xxx']);
308+
}
309+
```
310+
311+
Note: The built-in detectors does not require settings. This feature is only for convenience if you create a specific detector that requires settings.
312+
281313
---
282314

283315
If this library is useful for you, say thanks [buying me a beer :beer:](https://www.paypal.me/oscarotero)!
@@ -289,10 +321,8 @@ If this library is useful for you, say thanks [buying me a beer :beer:](https://
289321
[ico-sensiolabs]: https://insight.sensiolabs.com/projects/f0beab9f-fe41-47db-8806-373f80c50f9e/big.png
290322
[ico-downloads]: https://poser.pugx.org/embed/embed/downloads
291323
[ico-m-downloads]: https://poser.pugx.org/embed/embed/d/monthly
292-
[ico-references]: https://www.versioneye.com/php/embed:embed/reference_badge.svg?style=flat
293324

294325
[link-packagist]: https://packagist.org/packages/embed/embed
295326
[link-travis]: https://travis-ci.org/oscarotero/Embed
296327
[link-scrutinizer]: https://scrutinizer-ci.com/g/oscarotero/Embed/
297328
[link-sensiolabs]: https://insight.sensiolabs.com/projects/f0beab9f-fe41-47db-8806-373f80c50f9e
298-
[link-references]: https://www.versioneye.com/php/embed:embed/references

0 commit comments

Comments
 (0)