Reputation: 626
I'm not sure this is possible or not. I want a php script when executed , it will go to a page (on a different domain) and get the html contents of it and inside the html there's links , and that script is able to get each link's href.
html code:
<div id="somediv">
<a href="http://yahoo.com" class="url">Yahoo</a>
<a href="http://google.com" class="url">Google</a>
<a href="http://facebook.com" class="url">Facebook</a>
</div>
The output code(which php will echo out) will be
http://yahoo.com
http://google.com
http://facebook.com
I have heard of cURL in php can do something like this but not exactly like this , i'm a bit confused , i hope some can guide me on this.
Thanks.
Upvotes: 0
Views: 4144
Reputation: 9121
Using DOM and XPath:
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("http://www.example.com/"); // or you could load from a string using loadHTML();
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//div[@id='somediv']//a");
foreach($elements as $elem){
echo $elem->getAttribute('href');
}
BTW: you should read up on DOM and XPath.
Upvotes: 2
Reputation: 2912
have a look at something like http://simplehtmldom.sourceforge.net/
Upvotes: 3