dusterio / link-preview Goto Github PK
View Code? Open in Web Editor NEWLink preview generator for PHP with Laravel support
License: MIT License
Link preview generator for PHP with Laravel support
License: MIT License
If i install your package i get this message
Hello,
i try to use it in blade and i added app.php as alias
'LinkPreview' => Dusterio\LinkPreview\Client::class,
and in the blade file
$linkPreview = new LinkPreview($link);
But i get
ErrorException
Class 'Dusterio\LinkPreview\Client' not found
Hi,
Any way to set a custom user agent?
I see the HttpReader can take a config array, but I need to override that from the Client()
Any ideas?
Thanks for your your work on the library. Please support Laravel 10.
Hi,
I have installed plugin via composer and try to use it on Laravel 7, but I get class not found error if I using example code:
"Class 'Dusterio\LinkPreview\Client' not found"
I have checked other issues and followed the suggestion in the comment:
#22 (comment)
$client = new \Dusterio\LinkPreview\LinkPreview($url);
Then I got another class not found error:
"Class 'Dusterio\LinkPreview\LinkPreview' not found"
I have tried to clear cache and etc but no luck.
Any help much appreciated. Thanks
For the following link 500 error thrown.
https://www.revolution.watch/introducing-the-audemars-piguet-remaster-01/
But almost other links works fine. No issues. Can anyone help?
From this issue:
guzzle/guzzle#1841 (comment)
$guzzleResult->getBody()
GuzzleHttp\Psr7\Stream {#4825 ▼
-stream: stream resource @2055 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: 3391
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
Can anyone help? Thanks
When trying to get all previews for a YouTube or Vimeo URL, the request fails. It appears that the error is in Guzzle rather than your script, but perhaps there should be a way in the canParseLink
method of the HtmlParser
to check to see if it's a YouTube or Vimeo request first before trying to parse it.
Steps to reproduce (in Laravel):
Preview::setUrl('https://www.youtube.com/watch?v=mjMZwaqGPrc');
Preview::getPreviews(); // <-- script just dies without throwing exception.
Tracing this, I found that it is the HtmlParser
that is causing the script to fail. Particularly, Guzzle dies when trying to make the request in HttpReader:93
. The same thing happens with a Vimeo URL.
Hi,
I cannot used your library with Symfony > 3.4 (4 and 5) because dom-crawler and css-selector are in ~3.0...
Would it be possible to update these dependencies ?
Thanks !
Thanks in advance for this nice plugin.
$url = 'https://http.cat/404.jpg';
$linkPreview = new Client($url);
$parsedGeneral = $linkPreview->getPreview('general')->toArray();
Following is the response I get:
array:7 [▼
"cover" => GuzzleHttp\Psr7\Uri {#2588 ▼
-scheme: "https"
-userInfo: ""
-host: "http.cat"
-port: null
-path: "/404.jpg"
-query: ""
-fragment: ""
}
"images" => array:1 [▼
0 => GuzzleHttp\Psr7\Uri {#2588 ▶}
]
"title" => null
"description" => null
"video" => null
"videoType" => null
"url" => "https://http.cat/404.jpg"
]
Expected response:
cover: expected null or string, but GuzzleHttp\Psr7\Uri returned
images: expected [] empty array, but array of GuzzleHttp\Psr7\Uri returned
Can we expect expected data type? Thanks
Awesome library! I like the simplicity of it and the ability to modify it with my own parsers and what not.
I noticed that the default parser doesn't fetch data from facebook due to the meta tags using "name" instead of "content"
A quick fix:
protected function parseHtml($html)
{
$data = [
'image' => '',
'title' => '',
'description' => ''
];
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($html);
/** @var \DOMElement $meta */
foreach ($doc->getElementsByTagName('meta') as $meta) {
if($meta->hasAttribute('name')){
$prop = 'name';
}else{
$prop = 'property';
}
if ($meta->getAttribute($prop) === 'image') {
$data['image'] = $meta->getAttribute('content');
} elseif ($meta->getAttribute($prop) === 'og:image') {
$data['image'] = $meta->getAttribute('content');
} elseif ($meta->getAttribute($prop) === 'twitter:image') {
$data['image'] = $meta->getAttribute('value');
}
if ($meta->getAttribute($prop) === 'name') {
$data['title'] = $meta->getAttribute('content');
} elseif ($meta->getAttribute($prop) === 'og:title') {
$data['title'] = $meta->getAttribute('content');
} elseif ($meta->getAttribute($prop) === 'twitter:title') {
$data['title'] = $meta->getAttribute('value');
}
if ($meta->getAttribute($prop) === 'description') {
$data['description'] = $meta->getAttribute('content');
}else if ($meta->getAttribute($prop) === 'og:description') {
$data['description'] = $meta->getAttribute('content');
}
}
if (empty($data['title'])) {
/** @var \DOMElement $title */
foreach ($doc->getElementsByTagName('title') as $title) {
$data['title'] = $title->nodeValue;
}
}
return $data;
}
I double checked this against this Go script which does the same thing: https://github.com/badoux/goscraper/blob/master/goscraper.go
This also fixes an issue where the title is overwritten by the description because of a typo, and disables the need to loop through the meta tags again to find a description if the meta tag attribute is 'name' instead of 'property'.
URL: http://google.com
Response:
{
"cover": "/images/branding/googleg/1x/googleg_standard_color_128dp.png",
"images": [],
"title": "Google",
"description": "",
"video": "",
"videoType": ""
}
Code:
Preview::setUrl("http://google.com");
return Preview::getPreview('general')->toArray();
Some sites as demonstrated above give "strange" links, these are relative and prefixing with the url doesn't work either see:
http://google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
Is there any way to prevent this behaviour and make sure all links are absolute (since there is little to no use for having relative links with this library)
Hi,
I have problem running this package on Laravel 7 with guzzlehttp/guzzle 7.0.1. Could you please update it and make compatible with guzzlehttp/guzzle ^7.0.0. Thanks
Error:
- dusterio/link-preview v1.2.14 requires guzzlehttp/guzzle ^6.1 -> satisfiable by guzzlehttp/guzzle[6.1.0, 6.1.1, 6.2.0, 6.2.1, 6.2.2, 6.2.3, 6.3.0, 6.3.1, 6.3.2, 6.3.3, 6.4.0, 6.4.1, 6.5.0, 6.5.1, 6.5.2, 6.5.3, 6.5.4, 6.5.5, 6.5.x-dev]
Hi,
What do you think about adding a trim()
for the title
and description
attributes?
Sometimes the string parsed has some extra white spaces before and after...
Will try to look up what's happening in the next few days, but I did a composer update and now I get this:
Fatal error: Uncaught Error: Class 'DOMDocument' not found in vendor/symfony/dom-crawler/Crawler.php:192
Stack trace:
#0 vendor/symfony/dom-crawler/Crawler.php(170): Symfony\Component\DomCrawler\Crawler->addHtmlContent('<!doctype html>...', 'UTF-8')
#1 vendor/symfony/dom-crawler/Crawler.php(121): Symfony\Component\DomCrawler\Crawler->addContent('<!doctype html>...')
#2 vendor/symfony/dom-crawler/Crawler.php(70): Symfony\Component\DomCrawler\Crawler->add('<!doctype html>...')
#3 vendor/dusterio/link-preview/src/Parsers/HtmlParser.php(132): Symfony\Component\DomCrawler\Crawler->__construct('<!doctype html>...')
#4 vendor/dusterio/link-preview/src/Parsers/HtmlParser.php(100): Dusterio\LinkPreview\Parsers\HtmlParser->parseHtml(Object(Dusterio \LinkPreview\Models\Link))
#5 vendor/dusterio/link-preview/src/Client.php(61):
Dusteri in vendor/symfony/dom-crawler/Crawler.php on line 192
Not sure why it says "Dusteri" that last time. :P
I installed this library by composer in Laravel project.
I found the document is totally different and not working at all!
Error: Class 'Dusterio\LinkPreview\Client' not found!
Seems like composer still installing old kasp3r/link-preview package.
Also Laravel extra classes not working!
Can you please help? Thanks
Encountered this error on a certain link I tried. I would like to know if there is a work around for websites that give this error when you try to get a link preview from it. I have tested the link in both FB and LinkedIn, and both were able to retrieve the previews perfectly.
This is the code I am using to test:
$previewClient = new Client($url);
$previewClient->getParser('general')->getReader()->config(['connect_timeout' => 10, 'allow_redirects' => ['max' => 5]]);
try {
$preview = $previewClient->getPreview('general');
} catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
$preview = $e->getMessage();
} catch (\Dusterio\LinkPreview\Exceptions\MalformedUrlException $e) {
$preview = $e->getMessage();
} catch (\GuzzleHttp\Exception\ClientException $e) {
$preview = $e->getMessage();
}
$this->p($preview);
Thank you!
Hey there, thank you for this amazing lib :-) Link preview is not working anymore on Symfony 7 because it relies on dom-crawler v6 max. Is it possible to make it compatible please? Thank you!
I am getting an Error 999 when getting preview from LinkedIn articles. Any chances of having this good library of yours to work with LinkedIn?
Sometimes the examples refer to $client
, sometimes to $previewClient
.
I'm trying to test output with this:
$previewClient = new Client($url);
$previewClient->getParser('general')->getReader()->config(['connect_timeout' => 10, 'allow_redirects' => ['max' => 5]]);
try {
$preview = $previewClient->getPreview('general');
} catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
$preview = false;
} catch (\Dusterio\LinkPreview\Exceptions\MalformedUrlException $e) {
$preview = false;
}
... and it's giving "Fatal error: Uncaught Error: Call to undefined method Dusterio\LinkPreview\Client::getParser()". It also gives fatals for uncaught exceptions. :(
I was sort of blindly hoping this would be a nice tool for detecting if a site exists at an address, but it just takes a long time and then times out. Would that be an in-scope feature to add, or should I use more traditional means for that?
Would you be interested in adding support for a video tag on the HTMLParser?
I have a branch where I added a couple of tags to look for video tags (such as og:video
) for pages like imgur .gifv
gifs, which are just mp4 using the HTML5 <video>
tag, in such cases, you can embed the mp4 video instead of the much heavier gif or the inanimated jpg.
If it's fine with you let me know and I'll open a PR.
<pre>Array
(
[cover] => https://i.ytimg.com/vi/ycGfvA1vkR8/hqdefault.jpg
[images] => Array
(
)
[title] => NhÆ° Quỳnh - Duyên Pháºn (Thái Thá»�nh) PBN 99
[description] => DUYEN PHAN (Thai Thinh) NHU QUYNH PARIS BY NIGHT 99 Pháºn là con gái, chÆ°a má»�t lần yêu ai Nhìn vá»� tÆ°Æ¡ng lai mà thấy nhÆ° sông rá»�ng Ä�Æ°á»�ng dà i Cảnh nhà neo Ä�Æ¡n, ...
[video] =>
[videoType] => text/html
)
</pre>
php
Hello,
I have used link preview to fetch data as it was working well as i have some logical changes in my code due to requirement..previously i was checking through regular expression as if the so called text has link then with the help of preg_match i will retrieve it and it will parse to your function to get all info and after the logical changes in my code for the scenario if user will enter two urls if there text contains normal url and youtube url..
$fn_url="https://www.example.com";
if($fn_url != "")
{
$previewClient = new Client($fn_url);
$preview = $previewClient->getPreview('general');
$preview = $preview->toArray();
}
if i pass variable in new Client it throwing me error all the time and if i passed the same value as static string then it is giving output with so called info in array.previously it was working well
as the code was like..and it was working well..
if(preg_match(($reg_exUrl,$text,$match))
{
$previewClient = new Client($match[0]);
$preview = $previewClient->getPreview('general');
$preview = $preview->toArray();
}
so please guide me if i am making any mistake..
if it can be implemented in laravel 5.4 can i get example for it..
Thanks in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.