Skip to content

Instantly share code, notes, and snippets.

@hamidreza-s
Created April 20, 2013 13:55
Show Gist options
  • Save hamidreza-s/5426066 to your computer and use it in GitHub Desktop.
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!
<?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