I've written a script in php
to parse the link of each state located under the title High School Directory by State
of a table from this url. My first function fetch_item_links()
can extract those links in the right way. What I wish to do now is supply those urls within fetch_info()
function so that it will parse the red colored link
from the target page.
The second function also works flawlessly when I supply any individual url to test, as in this one.
However, when I try to run the whole script, I don't get any output. No error either.
This is my try so far:
<?php
$url = 'http://www.directoryofschools.com/high-schools/US.html';
$prefix = 'http://www.directoryofschools.com';
function fetch_item_links($link,$base)
{
$html_doc = new DOMDocument();
@$html_doc->loadHtmlFile($link);
$content_xpath = new DOMXPath($html_doc);
$item_row = $content_xpath->query('//*[@class="online_college_list"]//tr//td//a[@title]');
$packtBook = array();
for ($i=0; $i <$item_row->length; $i++){
$title = $item_row->item($i)->getAttribute('href') . "<br/>";
$string = $base . str_replace("..", "", $title);
$packtBook[] = $string;
}
return $packtBook;
}
function fetch_info($link)
{
$html_doc = new DOMDocument();
@$html_doc->loadHtmlFile($link);
$content_xpath = new DOMXPath($html_doc);
$item_row = $content_xpath->query('//*[@class="online_college_list"]//tr//td//a[@title]');
for ($i=0; $i <$item_row->length; $i++){
$title = $item_row->item($i)->getAttribute('href') . "<br/>";
echo $title;
}
}
$items = fetch_item_links($url,$prefix);
foreach($items as $file){
fetch_info($file);
}
?>
How can I make my script functional?