I am trying to create a foreach loop which would visit a link from an array of links and get contents from each of the visited websites.
But when I try to run it:
<?php
require('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach ($html->find('h2 a') as $title) {
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach ($html->find('.item-inner p img') as $img) {
$img_src[] = $img->getAttribute('src');
}
foreach ($links as $link) {
require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz'. $link);
foreach ($html-find('.item-page p') as $p) {
if (is_string($p))
$paragraph[] = $p->innertext;
}
}
?>
I get Fatal error: Cannot redeclare file_get_html()
And when I dont include the require('simple_html_dom.php');
I get: Fatal error: Uncaught Error: Call to undefined function find()
So I am realy confused.
Anyone got any ideas how to repaire this.
please change code few line
foreach ($links as $link) {
require('simple_html_dom.php'); **// here** comment line like **//require('simple_html_dom.php');**
$html = file_get_html('http://www.forgolf.cz'. $link);
foreach ($html-find('.item-page p') as $p) {
if (is_string($p))
$paragraph[] = $p->innertext;
}
}
?>
Finally Code
<?php
require ('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach($html->find('h2 a') as $title)
{
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach($html->find('.item-inner p img') as $img)
{
$img_src[] = $img->getAttribute('src');
}
foreach($links as $link)
{
// require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz' . $link);
foreach($html->find('.item-page p') as $p)
{
if (is_string($p)) $paragraph[] = $p->innertext;
}
}
?>
Change
require('simple_html_dom.php');
to
require_once('simple_html_dom.php');
in all instances of calling this function, not only in the within loop.