How to grab the title + images of a link when post

2019-05-10 14:52发布

问题:

I was wondering how to reproduce in PHP the comportment on Facebook:

When you share a link, it grabs the title of the page, and it automatically grabs the images from the page to decorate the link.

Is there anything already done like a plugin i can take a look at ?

Thank you.

回答1:

use PHP DOM classes to pull url content and parse the html to grab first <img> url and <h1> text.

UPDATE

use the DOM loadHTMLFile method to load the file into a DOMDocument object.

http://www.php.net/manual/en/domdocument.loadhtmlfile.php

use the DOM getELementsByTagName method to get the node contents.

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

use nodeValue method to get the node's value