Get contents of the URL and display links
I want to get contents of a URL and display only the links present on that page. Please help开发者_StackOverflow. Thanks
Use file_get_contents to get the contents, and using XPath via DOM or SimpleXML.
EDIT:
In both cases the URL needs to be the complete URL including the protocol, ie: http://google.com
. If what you posted in your comment to the other answer below is accurate this is your first problem. However the error your getting about the function being undefined means that the required libs for SimpleHtml havent been loaded before you make the call. Are you getting nay errors from your require
/include
statements?
Also when updating the question its best to post actual code in an edit to your original question so it can benefit from formatting.
Easiest way is to use SimpleXML
$htmlDoc = new SimpleXmlElement($url, null, true);
$anchors = $hmtlDoc->xpath('//a[@href]');
foreach($anchors as $a) {
$attr = $a->attributes();
echo sprintf('<a href="%s">%s</a>', $attr['href'], $a);
}
精彩评论