Hi, I want to get contents of a URL and display only the links present on that page. Please help. Thanks
A:
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);
}
prodigitalson
2010-08-05 04:41:26
Thanks for the help. I have tried the code but i am getting XML parsing errors. Please help
Zohaib
2010-08-05 08:16:11
+1
A:
Use file_get_contents to get the contents, and using XPath via DOM or SimpleXML.
Cary Chow
2010-08-05 04:45:25
if he has `allow_url_fopen` enabled theres no need to use `file_get_contents` with SimpleXml or the DOM unless he needs to handle the response, and if thats the case hes better off using CURL. Still +1 for raising the step :-)
prodigitalson
2010-08-05 04:47:44
I am using<code><?php$html = file_get_html('http://www.google.com/');foreach($html->find('a') as $element) echo $element->href . '<br>'; ?></code>But it gives the error, call to undefined fucntion file_get_html
Zohaib
2010-08-05 06:27:42
Thanks for the help. I have tried the code but i am getting XML parsing errors. Please help
Zohaib
2010-08-05 08:16:34