Giter Site home page Giter Site logo

link-preview's Introduction

LinkPreview Build Status

A PHP library to easily get website information (title, description, image...) from given url.

Dependencies

  • PHP >= 5.6 (without phpunit it should work on 5.5)
  • Guzzle

Installation

composer

To install LinkPreview with composer you need to create composer.json in your project root and add:

{
    "require": {
        "kasp3r/link-preview": "dev-master"
    }
}

Then run

$ wget -nc http://getcomposer.org/composer.phar
$ php composer.phar install

Library will be installed in vendor/kasp3r/link-preview

In your project include composer autoload file from vendor/autoload.php

Usage

use LinkPreview\LinkPreview;

$linkPreview = new LinkPreview('http://github.com');
$parsed = $linkPreview->getParsed();
foreach ($parsed as $parserName => $link) {
    echo $parserName . PHP_EOL . PHP_EOL;

    echo $link->getUrl() . PHP_EOL;
    echo $link->getRealUrl() . PHP_EOL;
    echo $link->getTitle() . PHP_EOL;
    echo $link->getDescription() . PHP_EOL;
    echo $link->getImage() . PHP_EOL;
    print_r($link->getPictures());
}

Output

general

http://github.com
https://github.com/
GitHub · Build software better, together.
GitHub is the best place to build software together. Over 10.1 million people use GitHub to share code.
https://assets-cdn.github.com/images/modules/open_graph/github-octocat.png
Array
(
    [0] => https://assets-cdn.github.com/images/modules/site/home-ill-build.png?sn
    [1] => https://assets-cdn.github.com/images/modules/site/home-ill-work.png?sn
    [2] => https://assets-cdn.github.com/images/modules/site/home-ill-projects.png?sn
    [3] => https://assets-cdn.github.com/images/modules/site/home-ill-platform.png?sn
    [4] => https://assets-cdn.github.com/images/modules/site/org_example_nasa.png?sn
)

###Youtube example

use LinkPreview\LinkPreview;
use LinkPreview\Model\VideoLink;

$linkPreview = new LinkPreview('https://www.youtube.com/watch?v=8ZcmTl_1ER8');
$parsed = $linkPreview->getParsed();
foreach ($parsed as $parserName => $link) {
    echo $parserName . PHP_EOL . PHP_EOL;

    echo $link->getUrl() . PHP_EOL;
    echo $link->getRealUrl() . PHP_EOL;
    echo $link->getTitle() . PHP_EOL;
    echo $link->getDescription() . PHP_EOL;
    echo $link->getImage() . PHP_EOL;
    if ($link instanceof VideoLink) {
        echo $link->getVideoId() . PHP_EOL;
        echo $link->getEmbedCode() . PHP_EOL;
    }
}

Output

youtube

https://www.youtube.com/watch?v=8ZcmTl_1ER8
http://gdata.youtube.com/feeds/api/videos/8ZcmTl_1ER8?v=2&alt=jsonc
Epic sax guy 10 hours
I had to remove my original one so I reuploaded this with much better quality.
(If you want it sound like previous one, try setting quality to 240p)
Yeah, I know that video sucks compared to original but no can do :(
http://i1.ytimg.com/vi/8ZcmTl_1ER8/hqdefault.jpg
8ZcmTl_1ER8
<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/8ZcmTl_1ER8" frameborder="0"/>

Todo

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

License

The MIT License (MIT)

Copyright (c) 2013 Tadas Juozapaitis [email protected]

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.

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.

link-preview's People

Contributors

edueo avatar kasp3r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

link-preview's Issues

Small improvement for readme

The utf8_decode( ) function willl make sure that for example Russian characters get displayed properly. This is almost crucial when dealing with content that needs to be previewed coming from anywhere in the world.

A sentence like: "Священная война (The Sacred War) - Alexandrov Red Army Choir" would otherwise show up as "СвÑ�Ñ�еннаÑ� война (The Sacred War) - Alexandrov Red Army Choir".

Upgrade Guzzle

Hi Kasp3r,

Can you upgrade Guzzle to 7x?

This is why it won't work on Laravel as it depends on a later version, Laravel 8x requires Guzzle ^7.0.1.

"guzzlehttp/guzzle": "^7.0.1"

Thanks

Derek

getImage() returning shorthand protocol instead of http/https

$filename = tempnam('/tmp', '');
file_put_contents($filename . '.jpg', file_get_contents($preview->getUrl()));

$preview->getUrl() returns //s.ytimg.com/yts/img/yt_1200-vfl4C3T0K.png which causes file_get_contents($preview->getUrl()) to fail.

Concatenating http: to getUrl() works.

Redirects/requests with cookies

I noticed today that some links are not parsed correctly. Eg. Akamai specifies a cookie a shows a redirect ('Location' header) and then the next page expects to see the cookie.

Now I assume that link-preview doesn't store/use cookies so it's unable to visit the page.

Given that Facebook and others do this just fine, I assume there are no legal/whatever requirements and this can be done.

Would you do it yourself or would you prefer a PR?

Whitespaces

Some websites include many whitespaces in their tags. For example see the title tag of https://www.fressnapf.de/p/penthouse-arthur.

Maybe apply a trim on all values? I did no pull request because there is another one open and maybe you have a better solution / more generic way or just want to include all whitespaces. So, just an issue and freedom to decide 💃

Laravel 6

Is this package support laravel 6.18? its not working on laravel version 6

Fatal error</b>: Class 'LinkPreview' not found

I'm including the autoload.php from my composer after adding "kasp3r/link-preview": "dev-master" to my composer.json but when i try to make an instance of the class i get the error:
Fatal error: Class 'LinkPreview' not found

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.