Created
April 20, 2013 13:55
-
-
Save hamidreza-s/5426066 to your computer and use it in GitHub Desktop.
With this simple script you can grab content of your desired web site by DOM class of it. Have Fun!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Set variables | |
$url = 'http://path/to/your/target'; | |
$className = 'foo'; | |
// Initialize CURL | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); | |
$html = curl_exec($curl); | |
curl_close($curl); | |
// Create DOM and XPATH | |
$dom = new DOMDocument(); | |
@$dom->loadHTML($html); | |
$xpath = new DomXpath($dom); | |
// Query through XPATH | |
$allXpathElements = $xpath->query("//*[@class='$className']"); | |
$allXpathElementsLength = $allXpathElements->length; | |
for ($i = 1; $i < $allXpathElementsLength; $i++) | |
{ | |
$allXpathElementsArray[] = $allXpathElements->item($i)->textContent; | |
} | |
// Fire! | |
var_dump($allXpathElementsArray); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment