I'm trying to do a code that will print the contents of all the elements with itemprop="price"
from some link but it don't work, I can't figure out why, this is the code:
<?php
error_reporting(0);
ini_set('display_errors', 0);
$doc = new DOMDocument();
$allscan = array(
'http://www.mobile54.co.il/30786',
'http://www.mobile54.co.il/35873',
'http://www.mobile54.co.il/34722'
);
$alllinks = array();
$html = file_get_contents($allscan[0]);
$doc->loadHTML($html);
$href = $doc->getElementsByTagName('a');
for ($j = 0; $j < count($allscan); $j++) {
$html = file_get_contents($allscan[$j]);
$doc->loadHTML($html);
$href = $doc->getElementsByTagName('a');
for ($i = 0; $i < $href->length; $i++) {
$link = $href->item($i)->getAttribute("href");
$lin = preg_replace('/\s+/', '', 'http://www.mobile54.co.il' . $link . "<br />");
if (strpos($link, 'items/') && !strpos($link, '#techDetailsAName')) {
if (!in_array($lin, $alllinks)) {
$alllinks[] = $lin;
}
}
}
}
for ($i = 0; $i < count($alllinks); $i++) {
echo $alllinks[$i];
}
for ($i = 0; $i < count($alllinks); $i++) {
$lin = "$alllinks[$i]";
$html = file_get_contents($lin);
$doc->loadHTML('<?xml encoding="UTF-8"?>' . $html);
$span = $doc->getElementsByTagName('span');
for ($j = 0; $j < $span->length; $j++) {
$attr = $span->item($j)->getAttribute('itemprop');
if ($attr == "price") {
echo $span->item($j)->textContent . "<br />";
}
}
}
?>
when I paste "someurl" insted of $lin
it work but the other way doesn't. I've tried to do $html = file_get_contents($alllinks[$i]);
but it didn't work, I don't know why.