Piece of code that queries EZA for a given string and grabs an article at random from the first results page. Again, this is slow as fuck and shouldn’t be used for production sites.
<?php
ini_set('error_reporting', 0);
$scrapeURL = 'http://ezinearticles.com/search/?q=';
$baseURL = 'http://ezinearticles.com';
$query = 'project+management';
$dom = new DOMDocument();
$dom->loadHTMLFile($scrapeURL . $query);
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
if ($div->getAttribute('class') == 'srch_title') {
foreach ($div->getElementsByTagName('a') as $a) {
$articles[] = $a->getAttribute('href');
}
}
}
$articleURL = $baseURL . $articles[rand(0,count($articles)-1)];
$dom->loadHTMLFile($articleURL);
$article['title'] = $dom->getElementsByTagName('h1')->item(0)->textContent;
$article['body'] = $dom->getElementById('body')->textContent;
print "<pre>";
print_r($article);
print "</pre>";
?>



