Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a specific user agent #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 15 additions & 145 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,159 +1,29 @@
# Link Preview
[![Build Status](https://travis-ci.org/dusterio/link-preview.svg)](https://travis-ci.org/dusterio/link-preview)
[![Code Climate](https://codeclimate.com/github/dusterio/link-preview/badges/gpa.svg)](https://codeclimate.com/github/dusterio/link-preview/badges)
[![Test Coverage](https://codeclimate.com/github/dusterio/link-preview/badges/coverage.svg)](https://codeclimate.com/github/dusterio/link-preview/badges)
[![Total Downloads](https://poser.pugx.org/dusterio/link-preview/d/total.svg)](https://packagist.org/packages/dusterio/link-preview)
[![Latest Stable Version](https://poser.pugx.org/dusterio/link-preview/v/stable.svg)](https://packagist.org/packages/dusterio/link-preview)
[![Latest Unstable Version](https://poser.pugx.org/dusterio/link-preview/v/unstable.svg)](https://packagist.org/packages/dusterio/link-preview)
[![License](https://poser.pugx.org/dusterio/link-preview/license.svg)](https://packagist.org/packages/dusterio/link-preview)

A PHP class that consumes an HTTP(S) link and returns an array of preview information. Think of Facebook sharing -
whenever you paste a link, it goes to specified page and fetches some details.
This is my first package published via a VCS (git in this case).

Initially based on [kasp3r/link-preview](https://github.com/kasp3r/link-preview) that seems to be abandoned.
I have added new lines to the source of the [original package][original].

Includes integrations with: Laravel 5
This package was created only for my personal use and learning purposes.

## Dependencies
___

* PHP >= 5.5
* Guzzle >= 6.1
* Symfony DomCrawler >= 3.0

## Installation via Composer

To install simply run:

```
composer require dusterio/link-preview
```

Or add it to `composer.json` manually:
Using a package directly from Github [requires to add a "repositories" attribute][info] to the `composer.json` for the project that requires the package.

```json
{
"require": {
"dusterio/link-preview": "~1.2"
// ...
"repositories": [
{
"type": "vcs",
"url": "https://github.com/JCarlosR/link-preview"
}
],
// ...
}
```

## Direct usage

```php
use Dusterio\LinkPreview\Client;

$previewClient = new Client('https://www.boogiecall.com/en/Melbourne');

// Get previews from all available parsers
$previews = $previewClient->getPreviews();

// Get a preview from specific parser
$preview = $previewClient->getPreview('general');

// Convert output to array
$preview = $preview->toArray();
```

**Output**

```
array(4) {
["cover"]=>
string(94) "https://cdn.boogiecall.com/media/images/872398e3d9598c494a2bed72268bf018_1440575488_7314_s.jpg"
["images"]=>
array(8) {
[0]=>
string(94) "https://cdn.boogiecall.com/media/images/872398e3d9598c494a2bed72268bf018_1440575488_7314_s.jpg"
[1]=>
string(94) "https://cdn.boogiecall.com/media/images/b18970cd4c808f4dcdf7c319779ab9c6_1457347623_2419_s.jpg"
}
["title"]=>
string(44) "Events, parties & live concerts in Melbourne"
["description"]=>
string(107) "List of events in Melbourne. Nightlife, best parties and concerts in Melbourne, event listings and reviews."
}
```

## Timeouts and errors

```php
// Default connect timeout is 5 seconds, you can change it to anything you want
$previewClient->getParser('general')->getReader()->config(['connect_timeout' => 3.14]);

// Default maximum redirect count is 10, but you can change it too
$previewClient->getParser('general')->getReader()->config(['allow_redirects' => ['max' => 10]]);

// If there is a network error (DNS, connect, etc), we throw ConnectionErrorException
try {
$previews = $previewClient->getPreviews();
} catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
echo "Oh no!";
}
```

### YouTube example

```php
use Dusterio\LinkPreview\Client;

$previewClient = new LinkPreview('https://www.youtube.com/watch?v=v1uKhwN6FtA');

// Only parse YouTube specific information
$preview = $previewClient->getPreview('youtube');

var_dump($preview->toArray());
```

**Output**

```
array(2) {
["embed"]=>
string(128) "<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/v1uKhwN6FtA" frameborder="0"/>"
["id"]=>
string(11) "v1uKhwN6FtA"
}
```

### Usage in Laravel 5

```php
// Add in your config/app.php

'providers' => [
'...',
'Dusterio\LinkPreview\Integrations\LaravelServiceProvider',
];

'aliases' => [
'...',
'Preview' => 'Dusterio\LinkPreview\Integrations\LaravelFacade',
];

// Set target url
Preview::setUrl('https://www.boogiecall.com');

// Get parsed HTML tags as a plain array
Preview::getPreview('general')->toArray();

// In case of redirects, see what the final url was
echo Preview::getUrl();
```

## Todo

1. Add more unit and integration tests
2. Update documentation
3. Add more parsers

## License

The MIT License (MIT)
Copyright (c) 2016 Denis Mysenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
I can remove that step uploading the package to packagist. But I won't do that because I just wanted to use the package with small changes.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[original]: https://github.com/dusterio/link-preview
[info]: https://getcomposer.org/doc/02-libraries.md
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "dusterio/link-preview",
"name": "jcarlosr/link-preview",
"type": "library",
"description": "Link preview generation for PHP with Laravel support",
"keywords": ["php","url","scraping","laravel","preview"],
"homepage": "http://github.com/dusterio/link-preview",
"homepage": "http://github.com/jcarlosr/link-preview",
"license": "MIT",
"authors": [
{
"name": "Denis Mysenko",
"email": "denis@mysenko.com",
"homepage": "https://www.mysenko.com"
"name": "Juan Ramos",
"email": "hola@programacionymas.com",
"homepage": "https://programacionymas.com"
}
],
"require": {
Expand Down
6 changes: 6 additions & 0 deletions src/Contracts/LinkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function getEffectiveUrl();
*/
public function setEffectiveUrl($effectiveUrl);

/**
* Get domain without path nor parameters
* @return string
*/
public function getBaseUrl();

/**
* Is this link an HTML page?
* @return boolean
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public function getEffectiveUrl()
return $this->effectiveUrl;
}

/**
* @inheritdoc
*/
public function getBaseUrl()
{
return '//' . parse_url($this->effectiveUrl, PHP_URL_HOST);
}

/**
* @inheritdoc
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Parsers/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ protected function parseHtml(LinkInterface $link)

if (!isset($cover) && count($images)) $cover = $images[0];

if (isset($cover) && substr($cover, 0, 1) === '/') // relative path
$cover = $link->getBaseUrl() . $cover; // absolute path

return compact('cover', 'title', 'description', 'images', 'video', 'videoType');
}
}
2 changes: 2 additions & 0 deletions src/Readers/HttpReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct($config = null)
'cookies' => $this->jar,
'connect_timeout' => 5
];

$this->config['headers']['User-Agent'] = 'Mozilla/5.0';
}

/**
Expand Down